PyInstaller executable fails to get source code of TorchScript
I'm trying to make Windows executable of my script that includes PyTorch. My script's imports are:

import numpy.core.multiarray  # which is a workaround for "ImportError: numpy.core.multiarray failed to import"
import six # which is workaround for "ModuleNotFoundError: No module named 'six'"
import torch
import torch.nn as nn
import warnings
import argparse
import json
import math
import numpy as np
import jsonschema
import os
from datetime import datetime
from sklearn.mixture import GaussianMixture
from scipy.io import wavfile
from scipy.signal import get_window
from scipy.signal import spectrogram


I'm using command:

pyinstaller --hidden-import pkg_resources.py2_warn extractor.py


PyInstaller throws no error while creating .exe, but when I run the .exe i get:

Traceback (most recent call last):
  File "site-packages\torch\_utils_internal.py", line 46, in get_source_lines_and_file
  File "inspect.py", line 967, in getsourcelines
  File "inspect.py", line 798, in findsource
OSError: could not get source code

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "extractor.py", line 3, in &lt;module&gt;
    import torch
  File "&lt;frozen importlib._bootstrap&gt;", line 991, in _find_and_load
  File "&lt;frozen importlib._bootstrap&gt;", line 975, in _find_and_load_unlocked
  File "&lt;frozen importlib._bootstrap&gt;", line 671, in _load_unlocked
  File "C:\ProgramData\Anaconda3\envs\forexe2\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\torch\__init__.py", line 367, in &lt;module&gt;
  File "&lt;frozen importlib._bootstrap&gt;", line 991, in _find_and_load
  File "&lt;frozen importlib._bootstrap&gt;", line 975, in _find_and_load_unlocked
  File "&lt;frozen importlib._bootstrap&gt;", line 671, in _load_unlocked
  File "C:\ProgramData\Anaconda3\envs\forexe2\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\torch\distributions\__init__.py", line 112, in &lt;module&gt;
  File "&lt;frozen importlib._bootstrap&gt;", line 991, in _find_and_load
  File "&lt;frozen importlib._bootstrap&gt;", line 975, in _find_and_load_unlocked
  File "&lt;frozen importlib._bootstrap&gt;", line 671, in _load_unlocked
  File "C:\ProgramData\Anaconda3\envs\forexe2\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\torch\distributions\von_mises.py", line 55, in &lt;module&gt;
  File "site-packages\torch\jit\__init__.py", line 1287, in script
  File "site-packages\torch\jit\frontend.py", line 164, in get_jit_def
  File "site-packages\torch\_utils_internal.py", line 53, in get_source_lines_and_file
OSError: Can't get source for &lt;function _rejection_sample at 0x0000000006892F70&gt;. TorchScript requires source access in order to carry out compilation, make sure original .py files are available. Original error: could not get source code
[5704] Failed to execute script extractor


Which I don't understand. This may be similar issue to this question. What is causing the problem?

I'm using conda env, with torch installed via pip (which is a workaround for the torch to be correctly hooked).


Windows 10
Python 3.8.2
torch 1.5.0+cu101
torchvision 0.6.0+cu101 (also tried with 0.2.2)
PyInstaller 3.6


Torch is open source, so you can search for the function _rejection_sample on the torch GitHub. This identifies the problematic file as torch.distributions.von_mises. If your program is not using the torch.distributions module, you can simply exclude it by changing the .spec file generated by pyinstaller.

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None
excluded_modules = ['torch.distributions'] # &lt;&lt;&lt; ADD THIS LINE

a = Analysis(['C:/your/path/here'],
             pathex=['C:\\your\\path\\here'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=excluded_modules,    # &lt;&lt;&lt; CHANGE THIS LINE
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
# remaining code omitted for brevity


You should only need to make changes in the two places specified above. The rest should already be there.

Then, build from the .spec file using

pyinstaller your_file.spec


In the future, consider using debug flags when you build your project. This also identifies the location of the file causing problems.
