14

我正在关注这篇文章,以便为所有 virtualenvs 制定一个 ipython 规则。

据我了解,这篇文章的主要思想是,当在 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

为了解决这个问题,我将它添加到我的 /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")

这个技巧适用于巴尔萨泽。但就我而言,我明白了

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

我想也许我也应该/usr/lib/python2.7/dist-packages/IPython/external加入sys.path/usr/bin/ipython我得到的是

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

此错误发生在 的第 43 行IPython/__init__py,但前一个错误发生在第 46 行。

  1. 如何使 ipython 与 virtualenv 一起工作?

  2. 如何添加/usr/lib/python2.7/dist-packages/IPython/externalsys.path导致新的错误?

  3. 为了更好地理解 python 导入路径,我应该阅读什么?

谢谢!

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']
4

9 回答 9

9

IPython 似乎现在可以工作了!

因为ipython找不到simplegeneric。我试图找到simplegeneric,发现simplegeneric 在'/usr/lib/pymodules/python2.7'中。添加“/usr/lib/pymodules/python2.7”后,ipython 工作正常。

Jeff Tratner 的评论真的很有帮助!

于 2012-07-20T08:13:57.077 回答
7

如果您ipython3使用安装apt-get,您将不会遇到任何此类问题。

而不是ipython,您应该ipython3在终端上键入。

对于 Python 2,请使用ipython2.

于 2013-06-18T17:29:55.653 回答
4

对路径进行更改没有任何帮助。但是以超级用户身份运行 ipython 是有效的(尽管我并不为此感到自豪!)

sudo ipython
于 2013-10-24T17:41:08.543 回答
3

pip install ipython而不是apt-get install ipython. 前者安装到 virtualenv 中,后者安装到您的系统 python 包中。

于 2016-02-23T19:34:32.817 回答
1

我有同样的问题,在我的情况下,原因是目录:/usr/lib/python2.7/dist-packages 在创建虚拟环境时没有添加到路径中(我想知道为什么)

在我的情况下,终端中的以下内容解决了这个问题:

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

注意:这假设您使用的是 virtualenvwrapper

于 2014-04-09T21:31:00.340 回答
0

就我而言,它是我自定义的 Python 路径中名为“path”的更高层的包,它导致 ImportError 来自:

import IPython.external.path as path

https://github.com/ipython/ipython/issues/3994

于 2014-02-07T20:18:42.727 回答
0

我在一个新的 ubuntu 主机上遇到了一些奇怪的事情,并意识到我在 mac 和 ubuntu 上的 mkvirtualenv 得到了不同的行为。我克服了这个差异

'mkvirtualenv --system-site-packages mynewenv'
于 2014-09-26T16:33:36.000 回答
0

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

于 2015-09-09T03:49:38.330 回答
0

我在成功安装时和安装后都traceback经历过类似的情况。python 3.5.4pip 10.0.1ipython

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'

更新或安装我得到的simplegenericiesudo pip install simplegeneric --upgrade

要求已经更新:/usr/lib/python3.5/site-packages (0.8.1) 中的 simplegeneric

经过一些初步试验后,我在 google/stackoverflow 上进行了搜索,看看是否有其他人遇到过类似的问题并找到了这个。它的@zjk 答案可以帮助我找到解决方案。

解决方案:在已安装的包目录中 仅找到egg文件夹。python3.5所以我删除了它们并全新安装了simplegeneric.

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

之后 ipython 就像魅力一样工作。

注意:我在系统级别安装软件包,这就是sudo需要的原因。

于 2018-04-21T05:35:22.647 回答