Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv. #10955

Closed
Pitometsu opened this issue Dec 20, 2017 · 15 comments

Comments

@Pitometsu
Copy link

Pitometsu commented Dec 20, 2017

/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/IPython/core/interactiveshell.py:726: UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
  warn("Attempting to work in a virtualenv. If you encounter problems, please "
Python 2.7.14 (default, Nov 14 2017, 19:52:12) 
Type "copyright", "credits" or "license" for more information.

IPython 5.5.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: python.el: native completion setup loaded

In [2]: import os, sys, IPython

In [3]: print os.environ['VIRTUAL_ENV']
print sys.executable
print IPython.__file__
print sys.path
/Users/netsu/.virtualenvs/csr-44kfYsMA

In [4]: /Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/bin/python2.7

In [5]: /Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/IPython/__init__.pyc

In [6]: ['/Users/netsu/.virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages', '', '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/bin', '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python27.zip', '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7', '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/plat-darwin', '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/plat-mac', '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/lib-tk', '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/lib-old', '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/lib-dynload', '/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages', '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/IPython/extensions', '/Users/netsu/.ipython']

What cause that warning?

@takluyver
Copy link
Member

If you install IPython outside a virtualenv, then activate the virtualenv and launch IPython, it lets you import modules from the env, for convenience. But this is not the same as running code properly inside the virtualenv, because it can access other modules from outside. So it warns you that if you really want to run code in the virtualenv, you should pip install ipython inside it.

@Pitometsu
Copy link
Author

@takluyver thank you for response.


In [27]: import sys; sys.executable
Out[27]: '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/bin/python2.7'

In [28]: 

It already in virtualenv, as well as python.

@Pitometsu
Copy link
Author


In [30]: !echo $PATH
/Users/netsu/.virtualenvs/csr-44kfYsMA/bin:/Users/netsu/Library/Python/2.7/bin:/usr/local/opt/python/libexec/bin:/usr/local/opt/openssl@1.1/bin:/usr/local/opt/sqlite/bin:/usr/local/opt/imagemagick@6/bin:/usr/local/opt/coreutils/libexec/gnubin:/usr/local/opt/texinfo/bin:/Users/netsu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin:/opt/X11/bin

In [31]: !ls
Pipfile  Pipfile.lock  README.md  csr  experiments  notebooks  setup.py

In [32]: !which ipython
/Users/netsu/.virtualenvs/csr-44kfYsMA/bin/ipython
In [33]: sys.path
Out[33]: 
['/Users/netsu/.virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages',
 '',
 '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/bin',
 '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python27.zip',
 '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7',
 '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/plat-darwin',
 '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/plat-mac',
 '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/lib-tk',
 '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/lib-old',
 '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/lib-dynload',
 '/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages',
 '/Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/lib/python2.7/site-packages/IPython/extensions',
 '/Users/netsu/.ipython',
 '/Users/netsu/Projects/csr']

@takluyver
Copy link
Member

Oh, sorry, I should have read your post more closely.

So the environment variable starts with /Users/netsu/.virtualenvs , but the executable path starts with /Users/netsu/.local/share/virtualenvs . I guess that's why it doesn't recognise that it's in the env. Is ~/.virtualenvs a symlink to ~/.local/share/virtualenvs ?

@Pitometsu
Copy link
Author

Pitometsu commented Dec 20, 2017

Yep. I guess, it created by pipenv

In [34]: !ls -l /Users/netsu/.virtualenvs
lrwxr-xr-x 1 netsu staff 37 Dec 15 10:32 /Users/netsu/.virtualenvs -> /Users/netsu/.local/share/virtualenvs

@takluyver
Copy link
Member

OK, I think you've hit a case we're not checking for. The code for it is here:

p = os.path.normcase(sys.executable)
paths = [p]
while os.path.islink(p):
p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
paths.append(p)
p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
# In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible
if p_venv.startswith('\\cygdrive'):
p_venv = p_venv[11:]
elif len(p_venv) >= 2 and p_venv[1] == ':':
p_venv = p_venv[2:]
if any(p_venv in p for p in paths):
# Running properly in the virtualenv, don't need to do anything
return

Do you want to make a pull request?

@Pitometsu
Copy link
Author

Thank you, @takluyver . But don't feel experienced enough in python yet, sorry. Could you do it, if know, how?

@proton1k
Copy link

@Pitometsu
are these links helpful ?
https://gist.github.com/dasgoll/e29999d42ce860c06476
https://stackoverflow.com/a/28167502/1598355
or just PR with patch as mentioned above ?

@takluyver
Copy link
Member

(I'm working on it - I think I've found a nicer, more robust way to do the check)

@Pitometsu
Copy link
Author

@takluyver thank you for working on it
But couldn't check to confirm your fix, unfortunately:

$ pipenv install -d -e git+https://github.com/takluyver/ipython@venv-check-samefile#egg=ipython
Installing -e git+https://github.com/takluyver/ipython@venv-check-samefile#egg=ipython…
Obtaining ipython from git+https://github.com/takluyver/ipython@venv-check-samefile#egg=ipython
  Cloning https://github.com/takluyver/ipython (to venv-check-samefile) to /Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/src/ipython
    Complete output from command python setup.py egg_info:

    IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2.
    When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
    Beginning with IPython 6.0, Python 3.3 and above is required.

    See IPython `README.rst` file for more information:

        https://github.com/ipython/ipython/blob/master/README.rst

    Python sys.version_info(major=2, minor=7, micro=14, releaselevel='final', serial=0) detected.



    ----------------------------------------

Error:  An error occurred while installing -e git+https://github.com/takluyver/ipython@venv-check-samefile#egg=ipython!
Command "python setup.py egg_info" failed with error code 1 in /Users/netsu/.local/share/virtualenvs/csr-44kfYsMA/src/ipython/

Could you make backport for 2.7 (ipython ver 5, I guess)?

@takluyver
Copy link
Member

Unfortunately a backport's not easy, because the samefile() function is Unix-only on Python 2, so we can't rely on it being there.

@Pitometsu
Copy link
Author

@takluyver it may use workaround for python 2 and alternative os like here https://github.com/mcepl/ropevim/commit/ab87505b4aff3695e1385a3e463a7f7d5c473ad9

@mei-li
Copy link

mei-li commented Mar 8, 2018

We also run into this warning, with a different usecase:

Through ipdb, with environment created through pyenv:

ipdb> import os, sys, IPython
ipdb> print(os.environ['VIRTUAL_ENV'])
/home/meili/.pyenv/versions/3.6.4/envs/radar364
ipdb> sys.executable
'/home/meili/.pyenv/versions/radar364/bin/python3'

@benmezger
Copy link

Would something like this work?

alias ipy="python -c 'import IPython; IPython.frontend.terminal.ipapp.launch_new_instance()'"

@Interesting6
Copy link

May be you should activate the virtual environment again.

millerdev added a commit to dimagi/commcare-hq that referenced this issue Apr 19, 2022
Resolving this probably requires an IPython fix. Related issue: ipython/ipython#10955

UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants