4

My ipython works okay, but when I try to open ipython console from venv I am getting:

Traceback (most recent call last):
  File "/usr/bin/ipython3", line 4, in <module>
    from IPython import start_ipython
ImportError: No module named 'IPython'

Any ways to do this?

pythad
  • 4,241
  • 2
  • 19
  • 41
  • 1
    that's using your system ipython - you sure you have activated the virtualenv? – scytale Aug 24 '15 at 13:10
  • @scytale, yes. Do I need to install ipython for each venv somehow? – pythad Aug 24 '15 at 13:13
  • 1
    in python 2, yes, you'd need to do `pip install ipython` - I'm not familiar with python3 but I imagine the same applies – scytale Aug 24 '15 at 13:14
  • If you installed ipython from Anaconda, you can create and manage virtual environments with conda. – Reblochon Masque Aug 24 '15 at 13:14
  • @pythad You need to [active the virtualenv](https://virtualenv.pypa.io/en/latest/userguide.html#activate-script) before running `pip install X` – turtlemonvh Aug 24 '15 at 13:14
  • @turtlemonvh, I've activated it, but ipython isn't a pip package(I installed it with `sudo apt-get install`) – pythad Aug 24 '15 at 13:17
  • @pythad That's probably the problem. When you install python packages with `apt-get` they're always going to go into your main python installation. If you want ipython running in a virtualenv, you can't use `apt-get` to install it. There are ways to [pick up the main installation of a package in your virtualenv](http://stackoverflow.com/questions/12079607/make-virtualenv-inherit-specific-packages-from-your-global-site-packages), but it's probably not worth the effort. – turtlemonvh Aug 24 '15 at 14:07

3 Answers3

1

Did you by any chance create the venv with the --system-site-packages flag and install ipython with pip? This combination is broken in my experience (Python 3.6.2, pip 9.0.1).

Two workarounds are:

  1. Use virtualenv instead of venv,
  2. Or use easy_install instead of pip.

With pip, scripts get the wrong shebang pointing to system python.

  • What is this I don't even. Well, this fixes it. But I am terrified that changing the name of the virtualenv from `venv` to `virtualenv` can have any effect at all. – Dunk Mar 15 '19 at 15:37
  • 1
    It's more than a name change. These are two separate packages (that work slightly differently) for creating virtual environments. – Geoffrey Ely Mar 17 '19 at 22:41
1

It seems that now it works. You should be able to achieve that:

user@host:~/ source path/to/venv/bin/activate
(venv) user@host:~/ python3 -m pip install ipython
(venv) user@host:~/ ipython
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.31.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import sys

In [2]: sys.executable
Out[2]: '/path/to/env/bin/python3'

In [3]:

Note that I am using python3 -m pip [...] to ensure that the module are installed in the environment and not elsewhere.

0

have you activated your virtualenv and installed ipython into the virtualenv?

source path/to/venv/bin/activate
pip install ipython
robguinness
  • 16,266
  • 14
  • 55
  • 65
scytale
  • 12,346
  • 3
  • 32
  • 46
  • Is ipython a pip package? – pythad Aug 24 '15 at 13:18
  • I am getting this: (.venv) $ pip install ipython3 Collecting ipython3 Could not find a version that satisfies the requirement ipython3 (from versions: ) No matching distribution found for ipython3 – robguinness Jun 07 '18 at 09:23
  • 3
    @robguinness - the comments aren't the place for asking for help - please open a new question – scytale Jun 07 '18 at 15:03
  • 1
    Seriously? I've been around awhile on SO. I don't think this merits a new question at all. My question is the same as the OP, but when I try the solution in your answer, I get an error. Seems to me like a comment is appropriate. – robguinness Jun 08 '18 at 12:24
  • 1
    @robguinness - that's not how SO works - nobody will see your question here except me cause this answer doesn't get much traffic. (I got a notification). I'm not about to provide 1 on 1 help. if you ask a new question it will appear in the appropriate places and will get seen. the comment's aren't the place to ask for new help. – scytale Jun 08 '18 at 13:17
  • I disagree. If I ask a new question which is very similar to this question, it will get marked as a duplicate. No one is forcing you to provide 1-on-1 help. – robguinness Jun 08 '18 at 14:07
  • 1
    just pointing out that SO doesn't work the way you think it does. anyway good luck with figuring that out. – scytale Jun 08 '18 at 16:53
  • When I install Ipython using this metodo all the module installed before are no longer available from ipython – G M Mar 11 '21 at 15:02