Я пытаюсь создать отдельный исполняемый файл, используя pyinstaller. Я использовал cython
для преобразования моего исходного кода, source_code.pyx
, в DLL-библиотеку с именем source_code.cp37-win_amd64.pyd
, написанную в python байт-коде. Затем я написал скрипт python, executable_code.py
, который импортирует DLL и выполняет ее основную функцию. Эти шаги были сделаны для защиты моего исходного кода при распространении исполняемого файла. Мой исходный код использует следующие библиотеки: numpy, pandas, tkinter, and sklearn
. Эти библиотеки должны быть включены как скрытые импорты в executable_code.spec
, который генерируется при запуске pyinstaller, для правильного запуска исполняемого файла. Если я не включаю скрытый импорт, pyinstaller успешно создает исполняемый файл. Однако, когда я пытаюсь запустить исполняемый файл, я получаю следующую ошибку: ModuleNotFoundError: No module named 'numpy'
. Чтобы устранить эту проблему, включив скрытый импорт, я попробовал 2 метода.
(Примечание: я использую Anaconda Powershell Prompt
для выполнения следующих команд.)
Первый метод:
Я запускаю следующую команду из Powershell:
pyinstaller --hiddenimport numpy --hiddenimport pandas --hiddenimport tkinter --hiddenimport sklearn --onefile C:\Users\abasi.brown.FDH-INC\Desktop\source-code-protection-tutorial\executable_code.py
Второй метод:
- Я запускаю pyinstaller без тегов
--hiddenimport
следующим образом:
pyinstaller --onefile C:\Users\abasi.brown.FDH-INC\Desktop\source-code-protection-tutorial\executable_code.py
Затем я использую текстовый редактор, чтобы открыть
executable_code.spec
. Затем я вставляю следующую строку кода в функцию
a = Analysis
:
hiddenimports=['numpy', 'pandas', 'tkinter', 'sklearn'],
Затем я выполняю следующую команду в Powershell:
pyinstaller .\executable_code.spec --specpath=test
Оба эти метода приводят к следующей ошибке:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\abasi.brown.FDH-INC\\AppData\\Roaming\\jupyter\\runtime\\kernel-0104f617-c86b-4f39-8304-4cfc20a4bfcc.json'
Когда я включаю только numpy
в качестве скрытого импорта, я не получаю эту ошибку, используя любой метод. Однако, когда я включаю другие библиотеки, я всегда получаю эту ошибку. Что я могу сделать, чтобы решить эту проблему?
Вот весь вывод, сгенерированный Powershell:
>>
>>
67 INFO: PyInstaller: 4.0.dev0+a1f92c6a08
68 INFO: Python: 3.7.4 (conda)
68 INFO: Platform: Windows-10-10.0.18362-SP0
69 INFO: wrote C:\Users\abasi.brown.FDH-INC\Desktop\New folder (3)\executable_code.spec
72 INFO: UPX is not available.
73 INFO: Extending PYTHONPATH with paths
['C:\\Users\\abasi.brown.FDH-INC\\Desktop\\source-code-protection-tutorial',
'C:\\Users\\abasi.brown.FDH-INC\\Desktop\\New folder (3)']
73 INFO: checking Analysis
213 INFO: Building because hiddenimports changed
214 INFO: Initializing module dependency graph...
220 INFO: Caching module graph hooks...
226 INFO: Analyzing base_library.zip ...
5141 INFO: Caching module dependency graph...
5268 INFO: running Analysis Analysis-00.toc
5283 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\python.exe
5581 INFO: Analyzing C:\Users\abasi.brown.FDH-INC\Desktop\source-code-protection-tutorial\executable_code.py
5583 INFO: Analyzing hidden import 'numpy'
8280 INFO: Processing pre-find module path hook distutils
8281 INFO: distutils: retargeting to non-venv dir 'c:\\users\\abasi.brown.fdh-inc\\appdata\\local\\continuum\\anaconda3\\lib'
8496 INFO: Processing pre-find module path hook site
8497 INFO: site: retargeting to fake-dir 'c:\\users\\abasi.brown.fdh-inc\\appdata\\local\\continuum\\anaconda3\\lib\\site-packages\\PyInstaller\\fake-modules'
11264 INFO: Processing pre-safe import module hook setuptools.extern.six.moves
20324 INFO: Analyzing hidden import 'pandas'
24585 INFO: Processing pre-safe import module hook six.moves
31767 INFO: Processing pre-safe import module hook urllib3.packages.six.moves
71251 INFO: Analyzing hidden import 'sklearn'
129809 INFO: Processing pre-safe import module hook win32com
143878 INFO: Processing module hooks...
143878 INFO: Loading module hook "hook-babel.py"...
143957 INFO: Loading module hook "hook-bokeh.py"...
144077 INFO: Loading module hook "hook-certifi.py"...
144079 INFO: Loading module hook "hook-Crypto.py"...
144092 INFO: Loading module hook "hook-cryptography.py"...
144413 INFO: Loading module hook "hook-cytoolz.itertoolz.py"...
144418 INFO: Loading module hook "hook-distutils.py"...
144419 INFO: Loading module hook "hook-docutils.py"...
146804 INFO: Loading module hook "hook-encodings.py"...
146902 INFO: Loading module hook "hook-h5py.py"...
146903 INFO: Loading module hook "hook-importlib_metadata.py"...
146905 INFO: Loading module hook "hook-IPython.py"...
147275 INFO: Excluding import 'PyQt4'
147282 INFO: Removing import of PyQt4 from module IPython.external.qt_loaders
147282 INFO: Excluding import 'matplotlib'
147289 INFO: Removing import of matplotlib.pyplot from module IPython.core.pylabtools
147289 INFO: Removing import of matplotlib._pylab_helpers from module IPython.core.pylabtools
147290 INFO: Removing import of matplotlib.figure from module IPython.core.pylabtools
147298 INFO: Removing import of matplotlib from module IPython.core.pylabtools
147301 INFO: Excluding import 'gtk'
147316 INFO: Excluding import 'tkinter'
147325 INFO: Removing import of tkinter from module IPython.lib.clipboard
147326 INFO: Excluding import 'PyQt5'
147341 INFO: Removing import of PyQt5.QtSvg from module IPython.external.qt_loaders
147341 INFO: Removing import of PyQt5.QtGui from module IPython.external.qt_loaders
147342 INFO: Removing import of PyQt5 from module IPython.external.qt_loaders
147344 INFO: Removing import of PyQt5.QtCore from module IPython.external.qt_loaders
147344 INFO: Removing import of PyQt5.QtWidgets from module IPython.external.qt_loaders
147354 INFO: Excluding import 'PySide'
147365 INFO: Removing import of PySide from module IPython.external.qt_loaders
147366 INFO: Loading module hook "hook-jedi.py"...
147500 INFO: Loading module hook "hook-jinja2.py"...
147517 INFO: Loading module hook "hook-jsonschema.py"...
147521 INFO: Loading module hook "hook-lib2to3.py"...
147524 INFO: Loading module hook "hook-llvmlite.py"...
147532 INFO: Loading module hook "hook-lxml.etree.py"...
147533 INFO: Loading module hook "hook-matplotlib.backends.py"...
148198 INFO: Matplotlib backend "GTK3Agg": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
148545 INFO: Matplotlib backend "GTK3Cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
148993 INFO: Matplotlib backend "MacOSX": ignored
cannot import name '_macosx' from 'matplotlib.backends' (c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\matplotlib\backends\__init__.py)
149833 INFO: Matplotlib backend "nbAgg": added
150380 INFO: Matplotlib backend "Qt4Agg": added
150727 INFO: Matplotlib backend "Qt4Cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
151273 INFO: Matplotlib backend "Qt5Agg": added
151620 INFO: Matplotlib backend "Qt5Cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
152115 INFO: Matplotlib backend "TkAgg": added
152600 INFO: Matplotlib backend "TkCairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
153086 INFO: Matplotlib backend "WebAgg": added
153536 INFO: Matplotlib backend "WX": ignored
No module named 'wx'
153882 INFO: Matplotlib backend "WXAgg": ignored
No module named 'wx'
154228 INFO: Matplotlib backend "WXCairo": ignored
No module named 'wx'
154583 INFO: Matplotlib backend "agg": added
154929 INFO: Matplotlib backend "cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
155396 INFO: Matplotlib backend "pdf": added
155859 INFO: Matplotlib backend "pgf": added
156218 INFO: Matplotlib backend "ps": added
156581 INFO: Matplotlib backend "svg": added
157032 INFO: Matplotlib backend "template": added
157494 INFO: Loading module hook "hook-matplotlib.py"...
157801 INFO: Loading module hook "hook-nbconvert.py"...
157817 INFO: Loading module hook "hook-nbformat.py"...
157822 INFO: Loading module hook "hook-notebook.py"...
158789 INFO: Loading module hook "hook-numba.py"...
158796 INFO: Excluding import 'IPython'
158806 INFO: Removing import of IPython.paths from module numba.caching
158806 INFO: Removing import of IPython.utils.path from module numba.caching
158809 INFO: Excluding import 'scipy'
158828 INFO: Removing import of scipy.linalg.cython_blas from module numba.targets.linalg
158828 INFO: Removing import of scipy.linalg.cython_lapack from module numba.targets.linalg
158830 INFO: Loading module hook "hook-numpy.core.py"...
158928 INFO: MKL libraries found when importing numpy. Adding MKL to binaries
158933 INFO: Loading module hook "hook-numpy.py"...
158934 INFO: Loading module hook "hook-openpyxl.py"...
158946 INFO: Loading module hook "hook-pandas.py"...
160376 INFO: Loading module hook "hook-PIL.Image.py"...
160840 INFO: Loading module hook "hook-PIL.py"...
160848 INFO: Excluding import 'PyQt4'
160855 INFO: Removing import of PyQt4 from module PIL.ImageQt
160856 INFO: Excluding import 'PySide'
160863 INFO: Removing import of PySide from module PIL.ImageQt
160867 INFO: Excluding import 'tkinter'
160874 INFO: Removing import of tkinter from module PIL.ImageTk
160875 INFO: Excluding import 'PyQt5'
160885 INFO: Removing import of PyQt5.QtCore from module PIL.ImageQt
160886 INFO: Removing import of PyQt5.QtGui from module PIL.ImageQt
160888 INFO: Import to be excluded not found: 'FixTk'
160896 INFO: Loading module hook "hook-PIL.SpiderImagePlugin.py"...
160904 INFO: Excluding import 'tkinter'
160912 INFO: Import to be excluded not found: 'FixTk'
160912 INFO: Loading module hook "hook-pkg_resources.py"...
161871 INFO: Excluding import '__main__'
161877 INFO: Removing import of __main__ from module pkg_resources
161878 INFO: Loading module hook "hook-pycparser.py"...
161882 INFO: Loading module hook "hook-pygments.py"...
163491 INFO: Loading module hook "hook-PyQt5.py"...
163863 WARNING: Hidden import "PyQt5.sip" not found!
163863 INFO: Loading module hook "hook-PyQt5.QtCore.py"...
163931 INFO: Loading module hook "hook-PyQt5.QtGui.py"...
164097 INFO: Loading module hook "hook-PyQt5.QtSvg.py"...
164522 INFO: Loading module hook "hook-PyQt5.QtWidgets.py"...
164717 INFO: Loading module hook "hook-pytest.py"...
165718 INFO: Loading module hook "hook-pythoncom.py"...
166295 INFO: Loading module hook "hook-pytz.py"...
166360 INFO: Loading module hook "hook-pywintypes.py"...
166922 INFO: Loading module hook "hook-scipy.io.matlab.py"...
166924 INFO: Loading module hook "hook-scipy.linalg.py"...
166935 INFO: Loading module hook "hook-scipy.py"...
166946 INFO: Loading module hook "hook-scipy.sparse.csgraph.py"...
166952 INFO: Loading module hook "hook-scipy.special._ellip_harm_2.py"...
166953 INFO: Loading module hook "hook-scipy.special._ufuncs.py"...
166957 INFO: Loading module hook "hook-setuptools.py"...
167821 INFO: Loading module hook "hook-sklearn.metrics.cluster.py"...
167822 WARNING: Hidden import "sklearn.utils.sparsetools._graph_validation" not found!
167830 WARNING: Hidden import "sklearn.utils.sparsetools._graph_tools" not found!
167831 INFO: Loading module hook "hook-sphinx.py"...
173335 INFO: Loading module hook "hook-sqlalchemy.py"...
173561 INFO: Found 4 sqlalchemy hidden imports
173562 WARNING: Hidden import "MySQLdb" not found!
173564 WARNING: Hidden import "psycopg2" not found!
175883 WARNING: Hidden import "sqlalchemy.sql.functions.func" not found!
175899 INFO: Import to be excluded not found: 'sqlalchemy.testing'
175899 INFO: Loading module hook "hook-sqlite3.py"...
176003 INFO: Loading module hook "hook-sysconfig.py"...
176005 INFO: Loading module hook "hook-tables.py"...
176007 INFO: Loading module hook "hook-win32com.py"...
176159 INFO: Loading module hook "hook-xml.dom.domreg.py"...
176159 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
176162 INFO: Loading module hook "hook-xml.py"...
176170 INFO: Loading module hook "hook-zmq.py"...
188212 WARNING: Hidden import "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\zmq\backend\cffi\__pycache__\_cffi_ext.c(213):" not found!
188332 WARNING: Hidden import "No" not found!
188333 WARNING: Hidden import "fatal" not found!
188335 WARNING: Hidden import "such" not found!
188342 WARNING: Hidden import "'sys/un.h':" not found!
188467 WARNING: Hidden import "or" not found!
188468 WARNING: Hidden import "file:" not found!
188470 WARNING: Hidden import "directory" not found!
188579 WARNING: Hidden import "file" not found!
188580 WARNING: Hidden import "Cannot" not found!
188582 WARNING: Hidden import "error" not found!
188590 WARNING: Hidden import "open" not found!
188593 WARNING: Hidden import "_cffi_ext.c" not found!
188594 WARNING: Hidden import "C1083:" not found!
188601 INFO: Loading module hook "hook-_tkinter.py"...
188825 INFO: checking Tree
188870 INFO: checking Tree
188885 INFO: Loading module hook "hook-gevent.py"...
189094 INFO: Determining a mapping of distributions to packages...
262466 WARNING: Unable to find package for requirement greenlet from package gevent.
262466 INFO: Packages required by gevent:
['cffi']
264768 INFO: Looking for ctypes DLLs
265072 INFO: Analyzing run-time hooks ...
265110 INFO: Including run-time hook 'pyi_rth_mplconfig.py'
265112 INFO: Including run-time hook 'pyi_rth_mpldata.py'
265114 INFO: Including run-time hook 'pyi_rth_pkgres.py'
265116 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
265123 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
265126 INFO: Including run-time hook 'pyi_rth_traitlets.py'
265128 INFO: Including run-time hook 'pyi_rth_certifi.py'
265134 INFO: Including run-time hook 'pyi_rth_pyqt5.py'
265137 INFO: Including run-time hook 'pyi_rth__tkinter.py'
265224 INFO: Looking for dynamic libraries
265269 WARNING: lib not found: impi.dll dependency of c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\Library\bin\mkl_blacs_intelmpi_ilp64.dll
265363 WARNING: lib not found: mpich2mpi.dll dependency of c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\Library\bin\mkl_blacs_mpich2_lp64.dll
265545 WARNING: lib not found: msmpi.dll dependency of c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\Library\bin\mkl_blacs_msmpi_ilp64.dll
265623 WARNING: lib not found: impi.dll dependency of c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\Library\bin\mkl_blacs_intelmpi_lp64.dll
265745 WARNING: lib not found: msmpi.dll dependency of c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\Library\bin\mkl_blacs_msmpi_lp64.dll
266220 WARNING: lib not found: mpich2mpi.dll dependency of c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\Library\bin\mkl_blacs_mpich2_ilp64.dll
266336 WARNING: lib not found: pgf90rtl.dll dependency of c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\Library\bin\mkl_pgi_thread.dll
266341 WARNING: lib not found: pgc14.dll dependency of c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\Library\bin\mkl_pgi_thread.dll
266346 WARNING: lib not found: pgf90.dll dependency of c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\Library\bin\mkl_pgi_thread.dll
268652 INFO: Looking for eggs
268653 INFO: Using Python library c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\python37.dll
268654 INFO: Found binding redirects:
[]
268709 INFO: Warnings written to C:\Users\abasi.brown.FDH-INC\Desktop\New folder (3)\build\executable_code\warn-executable_code.txt
269854 INFO: Graph cross-reference written to C:\Users\abasi.brown.FDH-INC\Desktop\New folder (3)\build\executable_code\xref-executable_code.html
270283 INFO: checking PYZ
270341 INFO: Building because toc changed
270341 INFO: Building PYZ (ZlibArchive) C:\Users\abasi.brown.FDH-INC\Desktop\New folder (3)\build\executable_code\PYZ-00.pyz
279639 INFO: Building PYZ (ZlibArchive) C:\Users\abasi.brown.FDH-INC\Desktop\New folder (3)\build\executable_code\PYZ-00.pyz completed successfully.
279829 INFO: checking PKG
279833 INFO: Building because toc changed
279833 INFO: Building PKG (CArchive) PKG-00.pkg
Cannot find ('share\jupyter\runtime\kernel-0104f617-c86b-4f39-8304-4cfc20a4bfcc.json', 'C:\Users\abasi.brown.FDH-INC\AppData\Roaming\jupyter\runtime\kernel-0104f617-c86b-4f39-8304-4cfc20a4bfcc.json', 1, 'x')
Traceback (most recent call last):
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\abasi.brown.FDH-INC\AppData\Local\Continuum\anaconda3\Scripts\pyinstaller.exe\__main__.py", line 9, in <module>
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\__main__.py", line 114, in run
run_build(pyi_config, spec_file, **vars(args))
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\__main__.py", line 65, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 729, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 676, in build
exec(code, spec_namespace)
File "C:\Users\abasi.brown.FDH-INC\Desktop\New folder (3)\executable_code.spec", line 33, in <module>
console=True )
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\building\api.py", line 436, in __init__
upx_exclude=self.upx_exclude
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\building\api.py", line 199, in __init__
self.__postinit__()
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\building\datastruct.py", line 160, in __postinit__
self.assemble()
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\building\api.py", line 284, in assemble
pylib_name=pylib_name)
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\archive\writers.py", line 331, in __init__
super(CArchiveWriter, self).__init__(archive_path, logical_toc)
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\archive\writers.py", line 63, in __init__
self._add_from_table_of_contents(logical_toc)
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\archive\writers.py", line 88, in _add_from_table_of_contents
self.add(toc_entry) # The guts of the archive.
File "c:\users\abasi.brown.fdh-inc\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\archive\writers.py", line 378, in add
fh = open(pathnm, 'rb')
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\abasi.brown.FDH-INC\\AppData\\Roaming\\jupyter\\runtime\\kernel-0104f617-c86b-4f39-8304-4cfc20a4bfcc.json'