python 3.4 py2exe image.open找不到图像
我正在尝试创建GUI的可执行文件(它使用来自我计算机的12个图像),但当我创建它时,.exe不会在另一台计算机上运行,因为它找不到这12个图像 我有一棵像这样的树python 3.4 py2exe image.open找不到图像,python,image,tkinter,exe,py2exe,Python,Image,Tkinter,Exe,Py2exe,我正在尝试创建GUI的可执行文件(它使用来自我计算机的12个图像),但当我创建它时,.exe不会在另一台计算机上运行,因为它找不到这12个图像 我有一棵像这样的树 project (folder) --mycode.py (python file) --setup.py (python file) --images (folder) --image1 (image .jpg) --image2 (image .jpg) --image3 (image .jpg) from dist
project (folder)
--mycode.py (python file)
--setup.py (python file)
--images (folder)
--image1 (image .jpg)
--image2 (image .jpg)
--image3 (image .jpg)
from distutils.core import setup
import py2exe
setup(
name="mycode",
windows =['mycode.py'],
options = {"py2exe": {"packages": ["encodings"]}}
)
然后我使用py2exe创建.exe,我的树现在是
project (folder)
--__pycache__ (folder)
--mycode.cpython-34.pyc
--dist (folder)
--mycode.exe (executable file for windows)
--(and other 22 elements)
--mycode.py (python file)
--setup.py (python file)
--images (folder)
--Im_0.jpg
--Im_1.jpg
--Im_2.jpg
--...
现在
我用这些来寻找我的图像
import os
scriptDir = os.path.dirname(os.path.realpath(__file__))
IM0 = os.path.join(scriptDir, 'Images\\Im_0.jpg')
....
然后我创建.exe
但是,当我运行可执行文件时,它会显示
"NameError: name '__file__' is not defined"
然后我试着去
因为py2exe没有文件属性
他们说使用
import os
import jpath
if hasattr(sys,"frozen") and sys.frozen in ("windows_exe", "console_exe"):
p=jpath.path(os.path.abspath(sys.executable)).dirname()
还有人说
import JPath class
import JPath.engine #@UnresolvedImport
import jpath.syntax
from jpath.data import Boolean, Item, List, Map, Null, Number, Pair, String
import jpath.errors as errors
from jpath.utils.messages import JPATH_INTERNAL_ERROR_MESSAGE
from jpath.utils.text import trimTo
import jpath.evaluators
import re
from linearclassification.lib.utils import jpath,chunk,contains,has_subsequence
但是python显示了这一信息
ImportError: No module named 'JPath'
现在我不知道如何运行我的代码,导入与我的树显示的.exe所在文件夹相同的图像,答案是这个 在mycode.py中我放了第一个
p=os.path.abspath(sys.executable)
IM0 = os.path.join('Images\\Im_0.jpg')
IM1 = os.path.join('Images\\Im_1.jpg')
IM2 = os.path.join('Images\\Im_2.jpg')
...
然后通过使用setup.py调用mycode.py来创建.exe
像这样
project (folder)
--mycode.py (python file)
--setup.py (python file)
--images (folder)
--image1 (image .jpg)
--image2 (image .jpg)
--image3 (image .jpg)
from distutils.core import setup
import py2exe
setup(
name="mycode",
windows =['mycode.py'],
options = {"py2exe": {"packages": ["encodings"]}}
)
然后转到命令提示符,将目录更改为setup.py和mycode.py所在的文件夹
我写的是后者
python setup.py py2exe
创建两个文件夹
--__pycache__ (folder)
--mycode.cpython-34.pyc
--dist (folder)
--mycode.exe (executable file for windows)
--(and other 22 elements)
现在,在dist(文件夹)中,我粘贴了所有图像所在的文件夹图像(文件夹)
--dist (folder)
--mycode.exe (executable file for windows)
--(and other 22 elements)
--Images (folder)
--Im_0.jpg
--Im_1.jpg
--Im_2.jpg
--....
完成
如果有人有更好的方法,请告诉我
无论如何,谢谢您是否在
setup.py
中指定了jpath
作为必需的模块?不,我想我应该在mycode.pyNope中指定它,您必须通过distutils
创建setup.py
(尽管您可能不需要使用setuptools
…不确定)。在my setup.py中,我从distutils.core导入设置导入py2exe设置(控制台=['mycode.py'])是正确的。您需要告诉设置
您需要什么模块。这就是setuptools
中的find_packages
非常方便的地方。