简体   繁体   English

一般来说IPython导入失败和python sys.path

[英]IPython import failure and python sys.path in general

I'm following this post to make one ipython rule all the virtualenvs. 我正在按照这篇文章制作一个ipython规则所有的virtualenvs。

From what I understand, the main idea of the post is that when in a virtualenv, ipython can not find its modules. 根据我的理解,该帖子的主要思想是,当在virtualenv中,ipython无法找到它的模块。

(a-virtualenv)me@pc:~$ ipython
Traceback (most recent call last):
  File "/usr/bin/ipython", line 19, in <module>
    from IPython.frontend.terminal.ipapp import launch_new_instance
ImportError: No module named IPython.frontend.terminal.ipapp

To work around this, I add this to my /usr/bin/ipython 要解决这个问题,我将其添加到我的/ usr / bin / ipython中

import sys
if "/usr/lib/python2.7/dist-packages" not in sys.path:
    sys.path.append("/usr/lib/python2.7/dist-packages")

This trick works for Balthazar. 这个技巧适用于Balthazar。 But in my case, I get this 但就我而言,我明白了

Traceback (most recent call last):
  File "/usr/bin/ipython", line 18, in <module>
    from IPython.frontend.terminal.ipapp import launch_new_instance
  File "/usr/lib/python2.7/dist-packages/IPython/__init__.py", line 46, in <module>
    from .frontend.terminal.embed import embed
  File "/usr/lib/python2.7/dist-packages/IPython/frontend/terminal/embed.py", line 37, in <module>
    from IPython.frontend.terminal.ipapp import load_default_config
  File "/usr/lib/python2.7/dist-packages/IPython/frontend/terminal/ipapp.py", line 38, in <module>
    from IPython.core.completer import IPCompleter
  File "/usr/lib/python2.7/dist-packages/IPython/core/completer.py", line 84, in <module>
    from IPython.utils import generics
  File "/usr/lib/python2.7/dist-packages/IPython/utils/generics.py", line 19, in <module>
    from IPython.external.simplegeneric import generic
  File "/usr/lib/python2.7/dist-packages/IPython/external/simplegeneric/__init__.py", line 4, in <module>
    from _simplegeneric import *
ImportError: No module named _simplegeneric

I thought maybe I should add /usr/lib/python2.7/dist-packages/IPython/external to sys.path in /usr/bin/ipython as well. 我想也许我应该将/usr/lib/python2.7/dist-packages/IPython/external添加到/usr/bin/ipython sys.path What I get is 我得到的是

Traceback (most recent call last):
  File "/usr/bin/ipython", line 18, in <module>
    from IPython.frontend.terminal.ipapp import launch_new_instance
  File "/usr/lib/python2.7/dist-packages/IPython/__init__.py", line 43, in <module>
    from .config.loader import Config
  File "/usr/lib/python2.7/dist-packages/IPython/config/loader.py", line 27, in <module>
    from IPython.utils.path import filefind, get_ipython_dir
  File "/usr/lib/python2.7/dist-packages/IPython/utils/path.py", line 24, in <module>
    from IPython.utils.process import system
  File "/usr/lib/python2.7/dist-packages/IPython/utils/process.py", line 27, in <module>
    from ._process_posix import _find_cmd, system, getoutput, arg_split
  File "/usr/lib/python2.7/dist-packages/IPython/utils/_process_posix.py", line 27, in <module>
    from IPython.utils import text
  File "/usr/lib/python2.7/dist-packages/IPython/utils/text.py", line 27, in <module>
    from IPython.external.path import path
ImportError: cannot import name path

This error occurred on line 43 of IPython/__init__py , but the previous one occurred on line 46. 此错误发生在IPython/__init__py第43行,但前一行发生在第46行。

  1. How can I make ipython work with virtualenv? 如何让ipython与virtualenv一起工作?

  2. How can adding /usr/lib/python2.7/dist-packages/IPython/external to sys.path resulting a new error? 如何将/usr/lib/python2.7/dist-packages/IPython/external添加到sys.path导致新的错误?

  3. What should I read to get a better understanding of python import path? 为了更好地理解python导入路径,我应该阅读什么?

Thanks! 谢谢!

PS: normal IPython sys.path PS:正常的IPython sys.path

['',
 '/usr/bin',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PIL',
 '/usr/lib/python2.7/dist-packages/gst-0.10',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/ubuntuone-client',
 '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel',
 '/usr/lib/python2.7/dist-packages/ubuntuone-couch',
 '/usr/lib/python2.7/dist-packages/ubuntuone-installer',
 '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol',
 '/usr/lib/python2.7/dist-packages/IPython/extensions']

IPython seems to work now! IPython现在似乎工作了!

Because ipython can not find simplegeneric. 因为ipython找不到simplegeneric。 I tried to locate simplegeneric and found simplegeneric is in '/usr/lib/pymodules/python2.7'. 我试图找到simplegeneric,发现simplegeneric在'/usr/lib/pymodules/python2.7'中。 After '/usr/lib/pymodules/python2.7' being added, ipython works fine. 在添加'/usr/lib/pymodules/python2.7'之后,ipython工作正常。

Jeff Tratner's comment really helps! Jeff Tratner的评论真有帮助!

If you install ipython3 using apt-get , you would not face any problems like that. 如果你使用apt-get安装ipython3 ,你就不会遇到任何问题。

Instead of ipython , you should type ipython3 on the terminal. 而不是ipython ,你应该在终端上键入ipython3

For Python 2 use ipython2 . 对于Python 2,使用ipython2

Making changes to the path did not help in any way. 改变路径并没有任何帮助。 However running ipython as a super user worked (although I'm not proud of it!) 然而,作为一个超级用户运行ipython工作(虽然我并不为此感到自豪!)

sudo ipython

pip install ipython instead of apt-get install ipython . pip install ipython而不是apt-get install ipython The former installs into the virtualenv, the latter installs into your system python packages. 前者安装在virtualenv中,后者安装到你的系统python包中。

I had the same problem, in my case the cause of it was that the directory: /usr/lib/python2.7/dist-packages was not added to the path when the virtual environment was created (I would like to know why) 我有同样的问题,在我的情况下,原因是目录:/usr/lib/python2.7/dist-packages没有添加到创建虚拟环境时的路径(我想知道原因)

In my case the following in the terminal solved the problem: 在我的情况下,终端中的以下解决了问题:

workon 'your_environment_here'
add2virtualenv /usr/lib/python2.7/dist-packages

Note: This assumes you are using virtualenvwrapper 注意:这假设您使用的是virtualenvwrapper

In my case, it was a package higher up in my customized Python path named "path" that was causing the ImportError from: 在我的例子中,它是一个更高的包,在我的自定义Python路径名为“path”,导致ImportError来自:

import IPython.external.path as path

See https://github.com/ipython/ipython/issues/3994 请参阅https://github.com/ipython/ipython/issues/3994

I experienced some weirdness in a new ubuntu host, and realized I was getting different behaviors for mkvirtualenv on mac versus the ubuntu. 我在一个新的ubuntu主机中经历了一些奇怪的事情,并意识到我在mac上与mbuvirtualenv的行为有所不同,而不是ubuntu。 I got past this difference with 我已经克服了这个差异

'mkvirtualenv --system-site-packages mynewenv'

对我来说,解决方案是 IPython从0.13.2 升级到目前最新的4.0.0。

Similar traceback I experienced with python 3.5.4 and pip 10.0.1 after installing ipython successfully. 类似的traceback我在成功安装ipython后遇到了python 3.5.4pip 10.0.1

line 32, in <module>
from .debugger import TerminalPdb, Pdb
File "/usr/lib/python3.5/site-packages/IPython/terminal/debugger.py", line 6, in <module>
    from IPython.core.completer import IPCompleter
File "/usr/lib/python3.5/site-packages/IPython/core/completer.py", line 137, in <module>
    from IPython.utils import generics
File "/usr/lib/python3.5/site-packages/IPython/utils/generics.py", line 8, in <module>
from simplegeneric import generic
ImportError: No module named 'simplegeneric'

updating or installing the simplegeneric ie sudo pip install simplegeneric --upgrade I get 更新或安装simplegenericsudo pip install simplegeneric --upgrade我得到

Requirement already up-to-date: simplegeneric in /usr/lib/python3.5/site-packages (0.8.1) 要求已经是最新的:/usr/lib/python3.5/site-packages(0.8.1)中的simplegeneric

After some initial trial I searched on google/stackoverflow to see if anyone else faced similar problem and found this. 经过一些初步试验后,我在google / stackoverflow上搜索,看看是否有其他人遇到过类似的问题并发现了这一点。 And its @zjk answer that helps me to find solution. 它的@zjk答案可以帮助我找到解决方案。

Solutions: Found only egg folder in the python3.5 installed package dir. 解决方案:python3.5安装包dir中找到只有egg文件夹。 So I removed them and fresh install the simplegeneric . 所以我删除了它们并重新安装了simplegeneric

sudo rm -rf /usr/lib/python3.5/site-packages/simplegeneric-0.8.1-py3.5.egg-info/
sudo pip install simplegeneric

after that ipython worked just like charm. 之后ipython就像魅力一样。

NB: I install the package at the system level that's why sudo required. 注意:我在系统级安装包,这就是sudo所需的原因。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM