无法导入xlrd模块
我的可执行脚本使用的软件包很少依赖于 xlrd 模块。因此,我尝试使用所示的 include 选项将此模块包含在安装脚本中。但是,当 runnery.py 调用包模块时,尽管library.zip 文件中存在xlrd 文件,但该模块无法导入xlrd。
from cx_Freeze import setup, Executable
import xlrd
buildOptions = dict(
compressed = True,
optimize=2,
path=sys.path+[".\\uitls", “.\\supported”],
include_files=[“Doc"],
includes=[“xlrd”, "win32com"],
packages=["utils", ”supported"],
append_script_to_exe=True,
copy_dependent_files=True,
)
setup(
name = "TestExecutable",
version = "0.1",
options = dict(build_exe = buildOptions),
executables = [Executable(
script=r".\\codebase\\ runner.py",
icon=".\\icon.ico",
base="Win32GUI")]
)
然而,如果我尝试在 runner.py 中导入 xlrd,它就能够导入它。我不确定在这种情况下出了什么问题,因为依赖包无法导入 xlrd。我是否缺少某些选项或我做错了什么?
更新: 我发现依赖包是通过生成一个进程来调用的,因此它创建了一个新环境,该环境没有加载 xlrd 模块,并且不知道包含它的library.zip。那么现在,我是否有可能使 xlrd 模块可用于 zip 文件中的包,即使它是通过生成新进程来运行的? 问候,
Few of the packages which my executable script is using are dependent on xlrd module. So I tried to include this module in the setup script by using the include option as shown. But when the runnery.py gives call to package modules, the module is not able to import xlrd though xlrd file are present in the library.zip file.
from cx_Freeze import setup, Executable
import xlrd
buildOptions = dict(
compressed = True,
optimize=2,
path=sys.path+[".\\uitls", “.\\supported”],
include_files=[“Doc"],
includes=[“xlrd”, "win32com"],
packages=["utils", ”supported"],
append_script_to_exe=True,
copy_dependent_files=True,
)
setup(
name = "TestExecutable",
version = "0.1",
options = dict(build_exe = buildOptions),
executables = [Executable(
script=r".\\codebase\\ runner.py",
icon=".\\icon.ico",
base="Win32GUI")]
)
Whereas if I try to import the xlrd in the runner.py, it is being able to import it. I not sure what is going wrong in this case, as the dependent packages are not being able to import xlrd. Is there some option that I am missing or something that I am doing wrong?
Update:
I found that the dependent package is called by spawning a process, so it creates a new environment, which do not have the xlrd module loded into it and is not aware of the library.zip containing it. So now, is it somehow possible for me to make the xlrd module available to the package from the zip file even if it ran by spwaning new process?
Regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
下载 xlrd 包时,您有两种选择:
xlrd-0.7.1\xlrd\
。python xlrd-0.7.1\setup.py install
(任一)。验证您是否可以导入它。打开 python 并输入
import xlrd
。如果没有看到错误,则说明已成功安装。When you download the xlrd package, you have two choices:
xlrd-0.7.1\xlrd\
in your Python Path.python xlrd-0.7.1\setup.py install
Once your done (either one). Verify that you can import it. Open up python and just type
import xlrd
. If you see no error, you know it has successfully installed.也许
path=sys.path+[".\\uitls", “.\\supported”],
中的uitls
应该是utils
。xlrd到底安装在哪里?
xlrd
是一个包,而不是一个模块;您是否尝试过将其放入软件包列表而不是包含列表中?一致地使用原始字符串。
为什么
script=r".\\codebase\\ runner.py",
中有双反斜杠? Windows 似乎将多个反斜杠视为一个,但为什么要冒险呢?为什么runner.py
之前有一个空格?您确定该安装文件确实运行吗?Perhaps the
uitls
inpath=sys.path+[".\\uitls", “.\\supported”],
should beutils
.Where exactly has xlrd been installed?
xlrd
is a package, not a module; have you tried putting it in the packages list instead of the includes list?Use raw strings consistently.
Why do you have doubled backslashes in
script=r".\\codebase\\ runner.py",
? Windows appears to regard multiple backslashes as one, but why chance it?? Why do you have a space beforerunner.py
? Are you sure that this setup file actually runs?找到了解决该问题的方法
我从 lib\site-packages 文件夹中复制了 xlrd 文件夹,并将其包含在 include_files 选项下。现在,当我的子进程可执行文件运行时,路径已设置,因此它知道 xlrd 的位置,并且可执行文件能够导入 xlrd。
Found a work around for the problem
I copied the xlrd folder form the lib\site-packages folder and included it under include_files option. Now when my subprocess executable is run, the path is set, so it knows the location of xlrd and the executable is able to import xlrd.
听起来 xlrd 模块没有正确安装。您可以从 Windows 中的命令提示符进行安装 pip install xlrd,这应该可以解决您的问题。我在使用 Pandas 读取扩展名为 xlsx 的文件时遇到了同样的问题。
Sounds like the xlrd module was not properly installed. You can install from the command prompt in Windows pip install xlrd, and this should fix your issue. I ran into the same issue when utilizing Pandas to read files with the extension xlsx.