使用pyinstaller把py文件打包成exe文件后,可以在没有安装python的电脑上运行py程序,真的很方便呢!
但是我不知道该如何导入自己写的module(sample_test),请帮忙看一下。
没有打包之前是可以正常运行的:
文件结构:
test1
├ TEMP
│ └ sample_test.py
└ callTest.py
callTest.py:
import sys
sys.path.append('./TEMP')
from sample_test import *
stdResult = eval("dispatch_character('input_test')")
print(stdResult)
sample_test.py:
def dispatch_character(argv):
return argv + '_ok'
运行【py callTest.py】输出结果:input_test_ok
使用pyinstaller生成exe文件之后找不到自己写的module(sample_test):
打包:
pyinstaller callTest.py --onefile
pyinstaller sample_test.py --onefile
文件结构:
test2
├ TEMP
│ └ sample_test.exe
└ callTest.exe
运行【callTest.exe】报错:
Traceback (most recent call last):
File "callTest.py", line 3, in <module>
ModuleNotFoundError: No module named 'sample_test'
[19324] Failed to execute script callTest
参考园子里的博文:Python打包方法——Pyinstaller
谢谢你的热心回答。虽然这篇博文没有解决我的问题,但是我参考了另一篇博文,把写法改变一下就可以导入多个py文件并同时导入自己写的module。
为了方便以后遇到同样问题的朋友,记录一下详细方法:
进入虚拟环境,然后输入【pyinstaller 要打包的py文件1 要打包的py文件1 --onefile】
(test) C:\TEST\bat\pyinstall_test\test>pyinstaller callTest.py ./TEMP/sample_test.py --onefile
63 INFO: PyInstaller: 3.4
63 INFO: Python: 3.6.2
64 INFO: Platform: Windows-10-10.0.17134-SP0
65 INFO: wrote C:\TEST\bat\pyinstall_test\test\callTest.spec
67 INFO: UPX is not available.
71 INFO: Extending PYTHONPATH with paths
['C:\\TEST\\bat\\pyinstall_test\\test',
'C:\\TEST\\bat\\pyinstall_test\\test\\TEMP',
'C:\\TEST\\bat\\pyinstall_test\\test']
72 INFO: checking Analysis
87 INFO: checking PYZ
96 INFO: checking PKG
97 INFO: Bootloader c:\test\bat\envinstall\test\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
98 INFO: checking EXE
98 INFO: Building because console changed
99 INFO: Building EXE from EXE-00.toc
100 INFO: Appending archive to EXE C:\TEST\bat\pyinstall_test\test\dist\callTest.exe
108 INFO: Building EXE from EXE-00.toc completed successfully.
进入dist文件夹中,执行生成的exe文件
(test) C:\TEST\bat\pyinstall_test\test>cd dist
(test) C:\TEST\bat\pyinstall_test\test\dist>callTest.exe
input_test_ok