简体   繁体   English

在virtualenv中,我还可以做些什么来解决不在python中导入而在ipython中导入的包的问题?

[英]What else can I do to troubleshoot a package not importing in python yet imports in ipython while in a virtualenv?

I am a Python enthusiast who decided to get serious about creating some of my own reusable packages. 我是一名Python爱好者,他决定认真地创建一些我自己的可重用软件包。

Synopsis of my problem: 我的问题简介:

  1. Calling python test_dummy.py fails to import a package eforest . 调用python test_dummy.py无法导入包eforest An ImportError is thrown. 抛出ImportError。
  2. import eforest in the python interpreter and in the ipython interpreter both throw no exceptions. 在python解释器和ipython解释器中import eforest都不会抛出异常。
  3. Calling run test_dummy.py within the ipython interpreter throws no exception. ipython解释器中调用run test_dummy.py 不会引发异常。
  4. All cases are run inside of a virtualenv. 所有案例都在virtualenv中运行。

virtualenv has nothing to do with my problem. virtualenv与我的问题无关。 I have posted a new question: script that adds packages to sys.path import as expected when run from ipython but throw exception when the script is run from python 我发布了一个新问题: 脚本在从ipython运行时按预期向sys.path导入添加包但在从python运行脚本时抛出异常

A similar question is at Can't import package from virtualenv 类似的问题是无法从virtualenv导入包

I read the documentation on virtualenv and virtualenvwrapper and installed them both on a Ubuntu 14.04.1 LTS system with Python 2.7.6. 我阅读了virtualenvvirtualenvwrapper上的文档,并使用Python 2.7.6将它们安装在Ubuntu 14.04.1 LTS系统上。

I created a virtualenv: 我创造了一个virtualenv:

mkvirtualenv module_troubleshooting -a . -r requirements.txt

My requirements.txt file contained: 我的requirements.txt文件包含:

Django==1.6.6
EasyProcess==0.1.6
PyVirtualDisplay==0.1.5
South==1.0
argparse==1.2.1
ipython==2.2.0
lxml==3.3.5
selenium==2.42.1
wsgiref==0.1.2

I activated my virtualenv with workon module_troubleshooting 我用workon module_troubleshooting激活了我的virtualenv

My virtualenv was active: (module_troubleshooting)dmmmd@ubuntuG5: 我的virtualenv是活跃的: (module_troubleshooting)dmmmd@ubuntuG5:

I understood from the virtualenvwrapper documentation that I could add packages to the sys.path via the add2virtualenv command. 我从virtualenvwrapper文档中了解到我可以通过add2virtualenv命令将包添加到sys.path。

Using add2virtualenv I added my very simple packages I had created. 使用add2virtualenv我添加了我创建的非常简单的包。 I verified that indeed the pth file contained those packages. 我确认pth文件确实包含那些包。

/home/dmmmd/development/python/singleton
/home/dmmmd/development/python/display
/home/dmmmd/development/python/browser
/home/dmmmd/development/python/eforest
/home/dmmmd/development/python/dummy

I ran ipython to see if I could import those packages. 我运行ipython以查看是否可以导入这些包。 I could. 我可以。 No errors. 没有错误。 Examining the sys.path in ipython revealed that the above paths were present. 检查ipython中sys.path显示上面的路径存在。

For my own edification I made the following module called test_dummy.py: 为了我自己的启发,我创建了以下模块test_dummy.py:

import browser
import display
import singleton
import eforest

I ran the following command in ipython : run test_dummy.py . 我在ipython中运行了以下命令: run test_dummy.py

No exceptions in ipython . ipython中没有例外。

I then exited ipython and ran the following command to assure myself that the global python was NOT being called and to see if python would run the script: /home/dmmmd/.virtualenvs/module_troubleshooting/bin/python test_dummy.py 然后我退出ipython并运行以下命令以确保自己没有调用全局python并查看python是否会运行脚本: /home/dmmmd/.virtualenvs/module_troubleshooting/bin/python test_dummy.py

Traceback (most recent call last):
    File "tests/test_dummy.py", line 4, in <module>
import eforest
ImportError: No module named eforest

Three other packages, browser, display, and singleton imported as expected and the exception was thrown at import eforest 按预期导入的其他三个包,浏览器,显示和单例,并在import eforest中抛出异常

At first, I theorized that eforest might contain some exception. 起初,我认为eforest可能包含一些例外。 So I ran /home/dmmmd/.virtualenvs/module_troubleshooting/bin/python and had an expected prompt and no import errors when importing eforest: 所以我运行/home/dmmmd/.virtualenvs/module_troubleshooting/bin/python并在导入eforest时遇到预期的提示并且没有导入错误:

Python 2.7.6 (default, Mar 22 2014, 22:57:26)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import eforest
>>> print eforest
<module 'eforest' from 'eforest/__init__.pyc'>
>>>

So then I theorized that the sys.path for ipython and python might be different. 所以我理论上说ipython和python的sys.path可能不同。 I saved the sys.path for python to a file and edited it to the following so the list from sys.path could be imported: 我将python的sys.path保存到文件中并将其编辑为以下内容,以便可以导入sys.path的列表:

/home/dmmmd/.virtualenvs/module_troubleshooting/bin/python -c "import sys; print sys.path" > path.py

I edited path.py so that from path import path would be the list sys.path from calling /home/dmmmd/.virtualenvs/module_troubleshooting/bin/python . 我编辑了path.py,因此from path import path将是调用/home/dmmmd/.virtualenvs/module_troubleshooting/bin/python的列表sys.path

I then ran ipython and typed the following commands: 然后我运行ipython并输入以下命令:

In [1]: from path import path

In [2]: path # this is the sys.path from calling /home/dmmmd/.virtualenvs/module_troubleshooting/bin/python
Out[2]:
['',
 '/home/dmmmd/development/python/singleton',
 '/home/dmmmd/development/python/eforest',
 '/home/dmmmd/development/python/dummy',
 '/home/dmmmd/development/python/display',
 '/home/dmmmd/development/python/browser',
 '/home/dmmmd/.virtualenvs/module_troubleshooting/lib/python2.7',
 '/home/dmmmd/.virtualenvs/module_troubleshooting/lib/python2.7/plat-powerpc-linux-gnu',
 '/home/dmmmd/.virtualenvs/module_troubleshooting/lib/python2.7/lib-tk',
 '/home/dmmmd/.virtualenvs/module_troubleshooting/lib/python2.7/lib-old',
 '/home/dmmmd/.virtualenvs/module_troubleshooting/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-powerpc-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/dmmmd/.virtualenvs/module_troubleshooting/local/lib/python2.7/site-packages',
 '/home/dmmmd/.virtualenvs/module_troubleshooting/lib/python2.7/site-packages']

To see if ipython 's sys.path was different I typed: 要查看ipythonsys.path是否不同,我输入:

In [5]: import sys

In [6]: len(sys.path)
Out[6]: 19

In [7]: len(path)
Out[7]: 16

In [8]: [item for item in sys.path if item not in path]
Out[8]:
['/home/dmmmd/.virtualenvs/module_troubleshooting/bin',
 '/home/dmmmd/.virtualenvs/module_troubleshooting/local/lib/python2.7/site-packages/IPython/extensions',
 '/home/dmmmd/.ipython']

The last two items of ipython 's sys.path are ipython related. ipython的sys.path的最后两项是ipython相关的。 The one different item is '/home/dmmmd/.virtualenvs/module_troubleshooting/bin'. 一个不同的项目是'/home/dmmmd/.virtualenvs/module_troubleshooting/bin'。

Can anybody offer any other troubleshooting advice or explain to me how having '/home/dmmmd/.virtualenvs/module_troubleshooting/bin' in the sys.path in ipython allows eforest to import as expected in ipython when run test_dummy.py is called? 任何人都可以提供任何其他故障诊断建议或向我解释,在IPython中的sys.path中有“/home/dmmmd/.virtualenvs/module_troubleshooting/bin”如何让eforest进口预期在IPython中 ,当run test_dummy.py叫?

eforest package: 森林包装:

eforest/
├── CustomElementFilters.py
├── CustomEtrees.py
├── CustomEtrees.pyc
├── __init__.py
└── __init__.pyc

Again, eforest imports as expected in ipython and with the python interpreter but it does not import when a script passed as the argument to /home/dmmmd/.virtualenvs/module_troubleshooting/bin/python test_dummy.py . 同样,eforest在ipython和python解释器中按预期导入,但是当脚本作为参数传递给/home/dmmmd/.virtualenvs/module_troubleshooting/bin/python test_dummy.py时,它不会导入。

The only possible exception to eforest that comes to my mind that is different from the other packages is that it imports lxml. 我想到的与其他软件包不同的唯一可能的例外是它导入lxml。 Could lxml depend somehow on having '/home/dmmmd/.virtualenvs/module_troubleshooting/bin' in the sys.path? lxml能否以某种方式依赖sys.path中的'/home/dmmmd/.virtualenvs/module_troubleshooting/bin'

Any suggestions for troubleshooting would be greatly appreciated. 任何有关故障排除的建议都将非常感谢。 I first noticed the problem when I was using py.test. 我在使用py.test时首先注意到了这个问题。 I thought it had something to do with py.test, but as it turned out the test scripts I was writing didn't run with python either. 我认为它与py.test有关,但事实证明我写的测试脚本也没有用python运行。 I decided to stop there and see if I could get some help. 我决定在那里停下来,看看能否得到一些帮助。

Ruled out virtualenv as a problem source 排除了virtualenv作为问题来源

I ran the following script with python test_dummy.py with the virtuenv deactivated . 我使用python test_dummy.py运行以下脚本,并停用 virtuenv。

import sys
import os
HOME = os.path.expanduser('~')
PYTHON = os.path.join(HOME, 'development/my_python')
PACKAGES = [
    'browser',
    'display',
    'singleton',
    'eforest'
]
for package in PACKAGES:
    package = os.path.join(PYTHON, package)
    if os.path.exists(package) is True:
        if package not in sys.path:
            print "loading package '{0}'".format(package)
            sys.path.append(package)
        else:
            print "package '{0}' already in sys.path".format(package)
    else:
        raise
import browser
import display
import singleton
import eforest

With the virtualenv deactivated eforest is the only package that does not import with running the python test_dummy.py command. 使用virtualenv 取消激活 eforest是唯一一个不运行python test_dummy.py命令导入的包。

Result: 结果:

Traceback (most recent call last):
  File "my_django/automated_browsing/tests/test_dummy.py", line 16, in <module>
import eforest
ImportError: No module named eforest

I found a solution although the solution does not solve the problem of why certain package paths added in an ad hoc fashion to sys.path are not imported when the script is run with python test_dummy.py . 我找到了一个解决方案,虽然该解决方案无法解决为什么在使用python test_dummy.py运行脚本时,不会导入以特定方式添加到sys.path某些包路径的问题。

I originally ran into this problem while trying to use py.test. 我在尝试使用py.test时遇到了这个问题。 In the py.test documentation I saw this tip about "managing your project with virutalenv, pip, and editable mode." 在py.test文档中,我看到了关于“使用virutalenv,pip和editable模式管理项目”的技巧 I ignored it because I thought it was too advanced for my level of Python knowledge. 我忽略了它,因为我觉得它对我的Python知识水平太高级了。

I decided to try the tip after reading about " creating your own python project ." 在阅读了“ 创建自己的python项目 ”之后,我决定尝试一下这个技巧。

In the pwd I created a setup.py file with the following code: 在pwd中,我使用以下代码创建了一个setup.py文件:

from setuptools import setup, find_packages

setup(name='import_troubleshooting', version='1.0')
packages = find_packages(exclude=[
    'my_django',
    'fake*',
    'tests*'
])

I then executed the following code at the command line: 然后我在命令行执行以下代码:

pip install -e .  # the pip way (which just calls "setup.py develop")

I then executed the following code with the expected results: 然后我用预期的结果执行以下代码:

$ python tests/test_dummy.py
# output    
package '/home/dmmmd/development/my_python/browser' already in sys.path
package '/home/dmmmd/development/my_python/display' already in sys.path
package '/home/dmmmd/development/my_python/singleton' already in sys.path
loading package '/home/dmmmd/development/my_python/test_a'
loading package '/home/dmmmd/development/my_python/hello_world'
loading 'browser'
loading 'display'
loading 'singleton'
loading 'test_a'
loading 'hello_world.hello'
Hello, world!

None of this explains to me why I couldn't add the package paths in an ad hoc fashion. 这些都没有向我解释为什么我无法以临时方式添加包路径。 I am pleased, however, to have learned something about python installation and packaging. 不过,我很高兴能够学到一些关于python安装和打包的知识。 The test_dummy.py script now runs as expected when called from python tests/test_dummy.py . python tests/test_dummy.py调用时, test_dummy.py脚本现在按预期运行。

I am now confused as to why three of the packages are already in sys.path when the test_dummy.py script is run: 我现在很困惑为什么在运行test_dummy.py脚本时,有三个软件包已经在sys.path了:

package '/home/dmmmd/development/my_python/browser' already in sys.path
package '/home/dmmmd/development/my_python/display' already in sys.path
package '/home/dmmmd/development/my_python/singleton' already in sys.path

I don't recall doing anything that would add those to sys.path but it is possible since I have been doing various tutorials. 我不记得做任何会将这些添加到sys.path东西,但是因为我一直在做各种教程。 They are not in sys.path in any other environment where I have not run the setup.py that I created. 在我没有运行我创建的setup.py任何其他环境中,它们不在sys.path中。

My confusion is a symptom of the complexity of Python, not a cause! 我的困惑是Python复杂性的一个症状,而不是原因!

Thank you for everybody's input. 谢谢大家的意见。

NB: This is the output of python test_dummy.py after a fresh virtualenv: 注意:这是一个新的virtualenv后python test_dummy.py的输出:

$ pip install -e .
# install output unremarkable
$ python tests/test_dummy.py
# output
loading package '/home/dmmmd/development/my_python/automated_browsing/browser'
loading package '/home/dmmmd/development/my_python/automated_browsing/display'
loading package '/home/dmmmd/development/my_python/automated_browsing/singleton'
loading package '/home/dmmmd/development/my_python/automated_browsing/hello_world'
loading package '/home/dmmmd/development/my_python/automated_browsing/test_a'
loading 'browser'
loading 'display'
loading 'singleton'
loading 'hello_world.hello'
Hello, world!
loading 'test_a'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果程序可以由python控制台运行而不能由ipython控制台运行,该怎么办? - what can I do if a program can be run by python console while can not be run by ipython console? 在 ipython notebook 上导入 imblearn python 包的问题 - Problems importing imblearn python package on ipython notebook 我怎么能解决这个cron工作? - How else can I troubleshoot this cron job? VirtualEnv python导入不起作用 - VirtualEnv python imports not working 我如何在没有pip或virtualenv的情况下安装python软件包 - How can I install a python package without pip or virtualenv 当 python package 安装在本地计算机上而不是 virtualenv 中时,如何在 virtualenv 上安装 pytest? - How do I install pytest on virtualenv when the python package is installed on the local machine and not in the virtualenv? 在这种情况下,如何拼写Python包/模块导入? - How do I spell Python package/module imports for this situation? 我可以跨同一个 python 包的模块重用导入吗? - Can I reuse imports across modules of the same python package? 如何解决ipython-listener和/或gedit的ipython插件问题? - How do I troubleshoot ipython-listener and/or gedit's ipython plugin? 导入包有什么作用? - what does importing a package do?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM