[英]How to identify which .pyd files are required for the execution of exe files generated using py2exe module
[英]Include .pyd module files in py2exe compilation
我正在尝试编译python脚本。 在执行exe时,我得到:-
C:\Python27\dist>visualn.exe
Traceback (most recent call last):
File "visualn.py", line 19, in <module>
File "MMTK\__init__.pyc", line 39, in <module>
File "Scientific\Geometry\__init__.pyc", line 30, in <module>
File "Scientific\Geometry\VectorModule.pyc", line 9, in <module>
File "Scientific\N.pyc", line 1, in <module>
ImportError: No module named Scientific_numerics_package_id
我可以在"C:\\Python27\\Lib\\site-packages\\Scientific\\win32"
位置看到文件Scientific_numerics_package_id.pyd。 我想将此模块文件包含在编译中。 我试图将上述文件复制到“ dist”文件夹中,但效果不佳。 任何想法?
更新:这是脚本:
from MMTK import *
from MMTK.Proteins import Protein
from Scientific.Visualization import VRML2; visualization_module = VRML2
protein = Protein('3CLN.pdb')
center, inertia = protein.centerAndMomentOfInertia()
distance_away = 8.0
front_cam = visualization_module.Camera(position= [center[0],center[1],center[2]+distance_away],description="Front")
right_cam = visualization_module.Camera(position=[center[0]+distance_away,center[1],center[2]],orientation=(Vector(0, 1, 0),3.14159*0.5),description="Right")
back_cam = visualization_module.Camera(position=[center[0],center[1],center[2]-distance_away],orientation=(Vector(0, 1, 0),3.14159),description="Back")
left_cam = visualization_module.Camera(position=[center[0]-distance_away,center[1],center[2]],orientation=(Vector(0, 1, 0),3.14159*1.5),description="Left")
model_name = 'vdw'
graphics = protein.graphicsObjects(graphics_module = visualization_module,model=model_name)
visualization_module.Scene(graphics, cameras=[front_cam,right_cam,back_cam,left_cam]).view()
Py2exe允许您通过includes
选项指定其他Python模块(.py和.pyd):
setup(
...
options={"py2exe": {"includes": ["Scientific.win32.Scientific_numerics_package_id"]}}
)
编辑。 如果Python能够执行以上操作
import Scientific.win32.Scientific_numerics_package_id
我在py2exe中遇到了类似的探针,我能找到的唯一解决方案是使用另一个工具将python转换为exe-pyinstaller
它非常易于使用,更重要,它可以正常工作!
UPDATE
从下面的评论中可以理解,由于导入错误,从命令行运行脚本也不起作用( 我的建议是先从命令行检查您的代码,然后尝试将其转换为EXE )
看来是PYTHONPATH问题。
PYTHONPATH是python程序用来查找导入模块的路径列表(类似于Windows PATH)。 如果您的脚本是从IDE运行的,则意味着在IDE中正确设置了PYTHONPATH,因此找到了所有导入的模块。
为了设置PYTHONPATH,您可以使用:
import sys|
sys.path.append(pathname)
或使用以下代码将path参数下的所有文件夹添加到PYTHONPATH:
import os
import sys
def add_tree_to_pythonpath(path):
"""
Function: add_tree_to_pythonpath
Description: Go over each directory in path and add it to PYTHONPATH
Parameters: path - Parent path to start from
Return: None
"""
# Go over each directory and file in path
for f in os.listdir(path):
if f == ".bzr" or f.lower() == "dll":
# Ignore bzr and dll directories (optional to NOT include specific folders)
continue
pathname = os.path.join(path, f)
if os.path.isdir(pathname) == True:
# Add path to PYTHONPATH
sys.path.append(pathname)
# It is a directory, recurse into it
add_tree_to_pythonpath(pathname)
else:
continue
def startup():
"""
Function: startup
Description: Startup actions needed before call to main function
Parameters: None
Return: None
"""
parent_path = os.path.normpath(os.path.join(os.getcwd(), ".."))
parent_path = os.path.normpath(os.path.join(parent_path, ".."))
# Go over each directory in parent_path and add it to PYTHONPATH
add_tree_to_pythonpath(parent_path)
# Start the program
main()
startup()
有一种方法可以解决我已经使用过多次的此类问题。 为了将额外的文件添加到py2exe结果中,您可以扩展媒体收集器以使其具有自定义版本。 以下代码是一个示例:
import glob
from py2exe.build_exe import py2exe as build_exe
def get_py2exe_extension():
"""Return an extension class of py2exe."""
class MediaCollector(build_exe):
"""Extension that copies Scientific_numerics_package_id missing data."""
def _add_module_data(self, module_name):
"""Add the data from a given path."""
# Create the media subdir where the
# Python files are collected.
media = module_name.replace('.', os.path.sep)
full = os.path.join(self.collect_dir, media)
if not os.path.exists(full):
self.mkpath(full)
# Copy the media files to the collection dir.
# Also add the copied file to the list of compiled
# files so it will be included in zipfile.
module = __import__(module_name, None, None, [''])
for path in module.__path__:
for f in glob.glob(path + '/*'): # does not like os.path.sep
log.info('Copying file %s', f)
name = os.path.basename(f)
if not os.path.isdir(f):
self.copy_file(f, os.path.join(full, name))
self.compiled_files.append(os.path.join(media, name))
else:
self.copy_tree(f, os.path.join(full, name))
def copy_extensions(self, extensions):
"""Copy the missing extensions."""
build_exe.copy_extensions(self, extensions)
for module in ['Scientific_numerics_package_id',]:
self._add_module_data(module)
return MediaCollector
我不确定哪个是Scientific_numerics_package_id模块,因此我假设您可以像这样导入它。 复制扩展方法将获得您遇到问题的其他模块名称,并将所有数据复制到dir文件夹中。 一旦有了,就可以使用新的媒体收集器,只需执行以下操作:
cmdclass ['py2exe'] = get_py2exe_extension()
这样就可以使用正确的扩展名。 您可能需要稍微触摸一下代码,但这应该是您所需要的一个良好的起点。
通过设置pythonpath并使用include函数,可以通过使用“ Gil.I”和“ Janne Karila”建议来纠正ImportError。 但是在此之前,我必须在两个模块的win32文件夹中创建__init__.py
文件。 顺便说一句,我仍然遇到上述脚本的另一个错误- 链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.