PYTHONPATH 与站点包之外的版本化包名称有关的问题
在尝试此处解决包管理问题时,我了解到我可能遇到问题我使用 PYTHONPATH 访问 site-packages 目录之外的包的方式。
当尝试从包的版本化目录文件夹名称(即 wx-2.8-msw-unicode
)导入模块(例如 wxPython
)时,我收到导入错误不在 python site-packages 目录中。
要重现问题:在 python 目录之外创建一个目录,例如 C:\foo
。下载 wxPython,并将其放置在该目录中(给出 C:\foo\wx-2.8-msw-unicode)。如果您愿意,可以添加更多。将目录 C:\foo
添加到您的 PYTHONPATH
环境变量中。打开一个新的 python 交互式 shell 并运行
import sys
for i in sys.paths:
print i
以验证路径是否存在(确实如此),然后
import wx
我收到导入错误。我做错了什么?这种行为正确吗?
In attempting to solve a package management issue here, I learned I may have issues with the way I'm using PYTHONPATH
to access packages outside of the site-packages directory.
I receive an import error when attempting to import modules from a package (say wxPython
) from its versioned directory folder name (i.e. wx-2.8-msw-unicode
) when said directory is not in the python site-packages directory.
To recreate the issue: Create a directory outside of the python directory, say C:\foo
. Download wxPython
, and place it in that directory (giving C:\foo\wx-2.8-msw-unicode
). Add more if you like. Add the directory C:\foo
to your PYTHONPATH
environment variable. Open a new python interactive shell and run
import sys
for i in sys.paths:
print i
to verify that the path is there (which it is), then
import wx
I receive an Import Error. What am I doing wrong? Is this behavior correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解您的意思,这是预期的行为。
C:\foo
位于您的 Python 路径中,但它不包含可导入的 wx 模块。为了成功导入 wx,它必须找到 wx.(py/pyc/pyd/dll/so) 或目录 wx 之一包含文件__init__.py
,直接位于 Python 路径上的目录之一中。安装程序通常会进行排序,确保它位于您可以导入的位置。如果您想自己执行此操作,则必须确保
wx
最终位于可导入的位置。As I understand what you're saying, this is expected behaviour.
C:\foo
is in your Pythonpath, but it does not contain an importable wx module. Forimport wx
to succeed, it has to find one ofwx.(py/pyc/pyd/dll/so)
or a directorywx
containing the file__init__.py
, directly in one of the directories on your Pythonpath.The installer will normally sort out making sure this is in an importable location for you. If you want to do it yourself, you have to ensure that
wx
ends up in an importable location.