Is iPython still virtualenv unaware?

214 views
Skip to first unread message

Xtian Simon

unread,
Apr 22, 2017, 8:14:12 PM4/22/17
to Project Jupyter
My question is about the specific situation of using iPython and python virtual environments (virtualenvwrapper).

Recently needed to install and use virtual environments; however, when I launch iPython, I get a lot of errors:

[TerminalIPythonApp] WARNING | Unknown error in handling startup files:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/lib/python3.6/site-packages/IPython/core/interactiveshell.py in safe_execfile(self, fname, *where, **kw)
   
2480                     fname, glob, loc,
-> 2481                     self.compile if kw['shell_futures'] else None)

[...]

AttributeError: 'filter' object has no attribute 'remove'



Searching the web for the issue, I found a few posts suggesting that iPython was "unaware" of virtual environments, Calling IPython from a virtualenv (c. 2012) but a commenter suggests that's no longer the case (2013).

The workaround in 2012 was to add some code to the iPython config file. And the OP in 'Calling iPython from a virtualenv' suggests another workaround is to install iPython inside each virtual environment. I really don't want to do that.

I don't see a lot of posts about iPython and virtual environments, so I'm thinking it's just me. So,
Is iPython still virtualenv unaware?

Xtian Simon

unread,
Apr 23, 2017, 4:03:57 PM4/23/17
to Project Jupyter
Should I post this issue to github.com/ipython?
If yes, can you suggest how I can improve the question for that group?

Thomas Kluyver

unread,
Apr 23, 2017, 4:35:19 PM4/23/17
to Project Jupyter
IPython is somewhat aware of virtualenvs - it will add them to sys.path so that you can import packages which are installed inside the virtualenv. But it can't make them work 100% - it can't isolate them, because it's using packages from outside the virtualenv, and it can't adapt to the Python version the virtualenv is created with. The error you show looks like you're running code written for Python 2 on Python 3.

If you need IPython to work entirely within a virtualenv, the recommendation is still to install it in the virtualenv. With recent versions of pip, this should be quick and painless.

Thomas

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/d27e1252-6ac2-476e-a8a7-3a584c3a9a66%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Xtian Simon

unread,
Apr 24, 2017, 10:32:01 AM4/24/17
to Project Jupyter
Thanks for the response Thomas.


The error you show looks like you're running code written for Python 2 on Python 3.
I haven't run any code yet. The errors I'm experiencing with iPython CLI are happening when I launch the application.
The environment is all from the Archlinux Python packages. There are Python2 and Python3 packages on the system, but on Arch you need to explicitly call python2.7 to use those packages. Python3 is the default environment.

I've been working on a project without virtual environment using iPython and Jupyter and Python3.
Only when I wanted to add a new module from PyPi and install it inside the VENV did I discover the problem.
I can run the code from the default python interpreter. It seems to only effect iPython.


Thomas Kluyver

unread,
Apr 24, 2017, 10:41:32 AM4/24/17
to Project Jupyter
Something appears to have added a startup file (see in ~/.ipython/profile_default/startup ) which IPython is trying to execute. What files are in that folder? Can you show the rest of the traceback?

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.

Xtian Simon

unread,
Apr 24, 2017, 7:06:02 PM4/24/17
to Project Jupyter

Something appears to have added a startup file (see in ~/.ipython/profile_default/startup ) which IPython is trying to execute. What files are in that folder?

[xtian@MasterShake .ipython]$ ll
total 28K
drwxr-xr-x  6 xtian xtian 4.0K Apr 24 18:06 .
drwxr-xr-x 50 xtian xtian 4.0K Apr 24 17:56 ..
drwxr-xr-x  2 xtian xtian 4.0K Jul 13  2014 extensions
drwxr-xr-x  3 xtian xtian 4.0K Aug 21  2014 nbextensions
drwxr-xr-x  7 xtian xtian 4.0K Apr 24 18:07 profile_default
drwxr-xr-x  9 xtian xtian 4.0K Aug 10  2014 profile_xtian.bak
-rw-r--r--  1 xtian xtian  195 Jul 13  2014 README


 
Can you show the rest of the traceback?
Here's an strace of the launch of ipython inside vevn
Here is the startup error message of ipython 


Xtian Simon

unread,
Apr 24, 2017, 7:11:43 PM4/24/17
to Project Jupyter

The error you show looks like you're running code written for Python 2 on Python 3.
My apologies. I realize now why you said this. My first link to the iPython error inside ipython was only the error after trying to run a script.
I've corrected this and made sure the iPython startup errors are included in the same Pastebin

Thomas Kluyver

unread,
Apr 25, 2017, 5:45:31 AM4/25/17
to Project Jupyter
OK, it looks like something has set sys.path like this:

sys.path = filter(...)

That won't work in Python 3, because filter() returns an iterator. You'll need to adapt it to:

sys.path = list(filter(...))

Unfortunately there's nothing to indicate where the code in question is. I don't believe it's part of IPython.

Thomas

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.

Xtian Simon

unread,
Apr 25, 2017, 10:26:19 PM4/25/17
to Project Jupyter
I hadn't thought to test python2 until you mentioned it.

Here's the command line call from inside a new test virtual environment to both iPython2 and Python3.
And the strace file for Python3 inside venv. No errors. And my code example runs Python3 in the test venv.

Any suggestions of tests I can run to eliminate iPython/Jupyter as the cause?
Reply all
Reply to author
Forward
0 new messages