如何在为Pycharm构建的项目运行pyinstaller时包含venv目录中的依赖项?

时间:2018-02-13 00:59:38

标签: python pycharm pyinstaller macos-high-sierra

我是Python和PyCharm的新手。我在OSX High Sierra上运行。

我在PyCharm中创建了一个使用lxml的项目。我已经将lxml 4.1.1安装为项目依赖项,我可以在myProject / venv / lib / site-package / lxml下看到它的文件

我可以在PyCharm中运行我的脚本没问题,但是我无法构建包含lxml库的可执行文件。

我从命令行使用pyinstaller 3.3.1。该项目构建,但是当我运行可执行文件时,我得到他的输出:

追踪(最近一次通话):   文件" analyze.py",第13行,in ImportError:没有名为lxml的模块 [4222]无法执行脚本分析

我一直在寻找一些命令行开关,告诉pyinstaller在venv目录下包含依赖项,没有运气。

非常感谢任何帮助......

这是我的构建命令和输出:

pyinstaller --clean --onefile analyze.py
51 INFO: PyInstaller: 3.3.1
51 INFO: Python: 2.7.10
60 INFO: Platform: Darwin-17.4.0-x86_64-i386-64bit
60 INFO: wrote /Users/gludwig/PycharmProjects/XML_Analyzer/analyze.spec
66 INFO: UPX is not available.
66 INFO: Removing temporary files and cleaning cache in /Users/gludwig/Library/Application Support/pyinstaller
76 INFO: Extending PYTHONPATH with paths
['/Users/gludwig/PycharmProjects/XML_Analyzer',
 '/Users/gludwig/PycharmProjects/XML_Analyzer']
76 INFO: checking Analysis
76 INFO: Building Analysis because out00-Analysis.toc is non existent
76 INFO: Initializing module dependency graph...
79 INFO: Initializing module graph hooks...
130 INFO: running Analysis out00-Analysis.toc
138 INFO: Caching module hooks...
141 INFO: Analyzing /Users/gludwig/PycharmProjects/XML_Analyzer/analyze.py
2363 INFO: Loading module hooks...
2364 INFO: Loading module hook "hook-httplib.py"...
2364 INFO: Loading module hook "hook-encodings.py"...
2874 INFO: Looking for ctypes DLLs
2874 INFO: Analyzing run-time hooks ...
2879 INFO: Looking for dynamic libraries
2995 INFO: Looking for eggs
2995 INFO: Using Python library /System/Library/Frameworks/Python.framework/Versions/2.7/Python
2996 INFO: Warnings written to /Users/gludwig/PycharmProjects/XML_Analyzer/build/analyze/warnanalyze.txt
3009 INFO: Graph cross-reference written to /Users/gludwig/PycharmProjects/XML_Analyzer/build/analyze/xref-analyze.html
3087 INFO: checking PYZ
3087 INFO: Building PYZ because out00-PYZ.toc is non existent
3087 INFO: Building PYZ (ZlibArchive) /Users/gludwig/PycharmProjects/XML_Analyzer/build/analyze/out00-PYZ.pyz
3331 INFO: Building PYZ (ZlibArchive) /Users/gludwig/PycharmProjects/XML_Analyzer/build/analyze/out00-PYZ.pyz completed successfully.
3403 INFO: checking PKG
3403 INFO: Building PKG because out00-PKG.toc is non existent
3403 INFO: Building PKG (CArchive) out00-PKG.pkg
5669 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
5685 INFO: Bootloader /Users/gludwig/Library/Python/2.7/lib/python/site-packages/PyInstaller/bootloader/Darwin-64bit/run
5685 INFO: checking EXE
5685 INFO: Building EXE because out00-EXE.toc is non existent
5685 INFO: Building EXE from out00-EXE.toc
5686 INFO: Appending archive to EXE /Users/gludwig/PycharmProjects/XML_Analyzer/dist/analyze
5703 INFO: Fixing EXE for code signing /Users/gludwig/PycharmProjects/XML_Analyzer/dist/analyze
5716 INFO: Building EXE from out00-EXE.toc completed successfully.

此外,pyinstaller会在build下生成一个警告文件,它包含以下内容:

missing module named org - imported by copy
missing module named _sha512 - imported by hashlib
missing module named _sha256 - imported by hashlib
missing module named _md5 - imported by hashlib
missing module named _sha - imported by hashlib
missing module named 'org.python' - imported by pickle
missing module named _subprocess - imported by subprocess
missing module named msvcrt - imported by subprocess, getpass
missing module named riscosenviron - imported by os
missing module named riscospath - imported by os
missing module named riscos - imported by os
missing module named ce - imported by os
missing module named _emx_link - imported by os
missing module named os2 - imported by os
missing module named nt - imported by os, ntpath
missing module named _winreg - imported by mimetypes, urllib
missing module named OverrideFrom23 - imported by Carbon.Res
missing module named SOCKS - imported by ftplib
missing module named rourl2path - imported by urllib
missing module named lxml - imported by /Users/gludwig/PycharmProjects/XML_Analyzer/analyze.py

我也尝试过以这种方式为venv中的库条目提供路径到pyinstaller:

pyi-makespec --paths =〜/ PycharmProjects / XML_Analyzer \        paths =〜/ PycharmProjects / XML_Analyzer / venv / lib / python2.7 / sitepackages / lxml analyze.py pyinstaller --onefile analyze.py

它没有帮助。

3 个答案:

答案 0 :(得分:4)

最后解决了这个问题,在同一条船上将其发布给其他人:

事实证明,当使用virtualenv并且您想要进行构建时,您必须添加环境变量PYTHONPATH以指向virtualenv目录的site-packages目录。就我而言,它看起来像这样:

PYTHONPATH="/Users/gludwig/PycharmProjects/XML_Analyzer/venv/lib/python2.7/site-packages"
export PYTHONPATH

为方便起见,我将此添加到virtualenv创建的激活文件中,因此当我激活virtualenv时它会自动设置。我还在停用部分添加了“未设置的PYTHONPATH”。

如果在virtualenv设置环境时自动完成,那将非常有用!

答案 1 :(得分:0)

乔治提供的答案使我朝着正确的方向前进,但我选择了另一条路;)

我使用了module add_two_values_task(output reg sum,output reg cout, input ain,input bin); task add_two_values; output [3:0] sum; output out; input [3:0]ain; input [3:0]bin; reg [3:0] sum; reg out; {out, sum} = ain + bin; endtask always @(ain or bin) begin add_two_values(cout,sum,ain,bin); end endmodule 命令行选项(请参阅here)来指示site-packages目录的位置,并为我解决了这个问题。 我正在使用Windows,并且想要一个可执行文件,所以这是命令行

--paths

答案 2 :(得分:0)

如果使用规格文件,则可以这样添加它:

a = Analysis(['myscript.py'],
             pathex=['.', ".\\path_to_libs", ".\\bla\\path_to_libs2"],
             hiddenimports=['pywinusb','CLogReport','otherLib'],
             ... )

如果您有多个文件和/或来自不同路径,这很有用。