无法导入xlrd模块

发布于 2024-10-20 09:25:52 字数 1218 浏览 8 评论 0原文

我的可执行脚本使用的软件包很少依赖于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何必那么矫情 2024-10-27 09:25:52

下载 xlrd 包时,您有两种选择:

  1. 在 Python 路径中包含 xlrd-0.7.1\xlrd\
  2. 完成后安装python xlrd-0.7.1\setup.py install

(任一)。验证您是否可以导入它。打开 python 并输入 import xlrd。如果没有看到错误,则说明已成功安装。

When you download the xlrd package, you have two choices:

  1. Include xlrd-0.7.1\xlrd\ in your Python Path.
  2. Install it 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.

一个人的旅程 2024-10-27 09:25:52

也许 path=sys.path+[".\\uitls", “.\\supported”], 中的 uitls 应该是 utils

xlrd到底安装在哪里?

xlrd 是一个包,而不是一个模块;您是否尝试过将其放入软件包列表而不是包含列表中?

一致地使用原始字符串。

为什么 script=r".\\codebase\\ runner.py", 中有双反斜杠? Windows 似乎将多个反斜杠视为一个,但为什么要冒险呢?为什么runner.py之前有一个空格?您确定该安装文件确实运行吗?

Perhaps the uitls in path=sys.path+[".\\uitls", “.\\supported”], should be utils.

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 before runner.py? Are you sure that this setup file actually runs?

楠木可依 2024-10-27 09:25:52

找到了解决该问题的方法
我从 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.

自找没趣 2024-10-27 09:25:52

听起来 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文