How does pip know about virtualenv and where to install packages - python

I am familiar with how a python virtualenv works and when we activate, it mainly modifies VIRTUAL_ENV and PATH variable. Primarily it adds the bin directory of virtualenv to system PATH and sets VIRTUAL_ENV to point to the virtual env root directory.
As an experiment I did this using virtualenv (version 1.11.6):
# Created two fresh virtual environments
virtualenv /tmp/env1
virtualenv /tmp/env2
echo $PATH
> SOME_PATH_VALUE
# Play with env1
source /tmp/env1/bin/activate
echo $PATH
> /tmp/env1/bin:SOME_PATH_VALUE
pip list
> pip, setuptools, wsgiref
pip install wget
> pip, setuptools, `wget`, wsgiref
# Obviously env2 has pip, setuptools and wsgiref only as of now
export PATH=/tmp/env2/bin:$PATH
pip list
> pip, setuptools, wsgiref
With my experiments it was solely depending on PATH variable for determining the installation path, but I am not sure if thats the whole picture. So the key questions are:
How does pip know where to install the requested package?
If my virtualenv is configured not to look for site-packages (ensured that the no-global-site-packages.txt file exists in correct location) and PATH variable is correctly set, but pip install still looks into system site-packages, then how to debug this?

Virtualenvs have the pip command installed in their bin/ directories. By swapping out the PATH you are executing a different pip command each time.
The bin/pip script is tied to the bin/python executable for that virtual env, and in turn that bin/python executable is tied to the virtualenv; you don't have to use source bin/activate to be able to use a virtualenv, because just the bin/python executable has all the information it needs.
You can see this by looking at the sys.prefix variable:
$ virtualenv-2.7 /tmp/env1
New python executable in /tmp/env1/bin/python2.7
Also creating executable in /tmp/env1/bin/python
Installing setuptools, pip...done.
$ virtualenv-2.7 /tmp/env2
New python executable in /tmp/env2/bin/python2.7
Also creating executable in /tmp/env2/bin/python
Installing setuptools, pip...done.
$ env1/bin/python -c 'import sys; print sys.prefix'
/private/tmp/env1
$ env2/bin/python -c 'import sys; print sys.prefix'
/private/tmp/env2

Related

I can't create a virtual environment set only to python3 in ubuntu

When I want to create a virtual environment with python3 (well python3.6 to be more precise) it keeps installing both python version even when I explicitly stated for it not to.
I used the command:
virtualenv -p /usr/bin/python3.6 trydjango/
Output:
Running virtualenv with interpreter /usr/bin/python3.6
Already using interpreter /usr/bin/python3.6
Using base prefix '/usr'
New python executable in /home/oren/Dev/Python-
Dev/trydjango/bin/python3.6
Also creating executable in /home/oren/Dev/Python-
Dev/trydjango/bin/python
Installing setuptools, pip, wheel...
done.
When I look at the bin directory, I expected only python3.6 installed but this is the output:
activate activate.ps1 easy_install pip3 python3
wheel
activate.csh activate_this.py easy_install-3.6 pip3.6 python3.6
activate.fish activate.xsh pip python python-config
I figured out the problem, I used both
pip install virtualenv and pip3 install virtualenv. I uninstalled both and only used pip3 install virtualenv. So now it only uses python3.
In the bin folder, both python and python3 are just symbolic links to python3.6. This is for convenience so you don't have to always type python3.6.
You can test this by using python --version to see what version actually runs.

Get path to original python executable (not the virtualenv)

Inside a virtualenv, sys.executable gives /path/to/venv/bin/python. How do I get the path of the python that the virtualenv was created from, such as /usr/bin/python3?
For example,
$ cd /tmp
$ virtualenv -p /usr/bin/python3 venv
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /tmp/venv/bin/python3
Also creating executable in /tmp/venv/bin/python
Installing setuptools, pip, wheel...
done.
$ venv/bin/python -c 'import sys; print(sys.executable)'
/tmp/venv/bin/python
The answer that I want is /usr/bin/python3.
In a virtualenv, sys.real_prefix is the directory of the actual Python installation being used by the virtualenv.
Other variables worth checking are sys.base_prefix and sys.base_exec_prefix.
Example code to run in virtualenv:
import sys
print(sys.base_prefix)

Error message for virtualenvwrapper on Mac OS X Yosemite 10.10.3

I recently did a clean install of Mac OS 10.10.3 and am trying to set up a python environment. But I can't get virtualenvwrapper to work.
I installed python 2 and 3 using Homebrew.
$ which python
/usr/local/bin/python
$ which python3
/usr/local/bin/python3
$ which pip
/usr/local/bin/pip
$ which virtualenv
/usr/local/bin/virtualenv
$ which virtualenvwrapper.sh
/usr/local/bin/virtualenvwrapper.sh
Here are the lines in my .bash_profile
export PATH=/usr/local/bin:$PATH
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
The virtualenv works on its own with: $ source bin/activate
But when I source the virtualenvwrapper.sh. I get the following error message.
/usr/local/opt/python/bin/python2.7: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python and that PATH is
set properly.
I can't figure out what went wrong. When I echo VIRTUALENVWRAPPER_PYTHON in the terminal. It returns the right path. In fact, all my paths are correct. They all point to the homebrew directory.
How can I get it to work?
It has been some time since this question was asked, but encountering the same issue, I'd like to add my solution in case someone might find it usefull:
Since virtualenvwrapper was installed using Python3, it is axpected that VIRTUALENVWRAPPER_PYTHON variable will point to Python3 path and not the Python2 path.
That is, in .bash_profile change:export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
To:export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
Then you can start using virtualwrapper, even if not installed for Python2 (unless you want to explicitly create the environment using Python2 in which case you'd probebly have to install it using pip2 and specify the relevant python path on the virtual environment creation command).
Per our comment exchange, you've installed virtualenvwrapper only on
python 3. You need to also install it on python2. To do so:
curl -O https://bootstrap.pypa.io/get-pip.py python2.7 ./get-pip.py
--user ~/.local/bin/pip install virtualenvwrapper My own setup is the opposite -- I use python2 for work, so install virtualenv for 3.x in
~/local -- hope that helps.
Thank you very much. Your answer points me to the right direction.
I solved the problem by uninstalling all things virtualenv.
pip uninstall virtualenv virtualenvwrapper
Then I installed everything again using pip2.7.
pip2.7 install virtualenv virtualenvwrapper
Now the terminal doesn't complain about can't find the module when I source virtualwrapper.sh
. /usr/local/bin/virtualenvwrapper.sh
However, python3 is the default python.
$ mkvirtualenv myenv
Using base prefix '/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4'
New python executable in myenv/bin/python3.4
Also creating executable in myenv/bin/python
Installing setuptools, pip...done.
To create a python2 env. I need to do this:
$ mkvirtualenv myenv --python /usr/local/bin/python
Running virtualenv with interpreter /usr/local/bin/python
New python executable in myenv/bin/python2.7
Also creating executable in myenv/bin/python
Installing setuptools, pip...done.
Per our comment exchange, you've installed virtualenvwrapper only on python 3. You need to also install it on python2. To do so:
curl -O https://bootstrap.pypa.io/get-pip.py
python2.7 ./get-pip.py --user
~/.local/bin/pip install virtualenvwrapper
My own setup is the opposite -- I use python2 for work, so install virtualenv for 3.x in ~/local -- hope that helps.
This has fixed my issue
Assuming you have already added an alias for python2 (the default version of python in mac)
Add these on ~/.bash_profile
export WORKON_HOME=~/.virtualenvs
VIRTUALENVWRAPPER_PYTHON='/usr/local/bin/python3'
source /usr/local/bin/virtualenvwrapper.sh
just modify the VIRTUALENVWRAPPER_PYTHON environment variable into this:
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
and then run it by typing
source ~/.bash_profile

Pip within virtualenv is global pip

I've got a virtualenv installation that seems a bit broken.
When I create a virtualenv using virtualenv .
It says everything is fine:
New python executable in ./bin/python2.7
Also creating executable in ./bin/python
Installing setuptools, pip...done.
I then source bin/activate to activate the virtualenv.
However, when I do which pip, I get the global pip. I take a peak into the bin/ folder and notice that pip isn't actually installed, so I can't even point to it directly.
I then tried to install pip in the virtualenv using easy_install, but it turns out that even that easy_install is pointing to the global easy_install, and surprise, there is no easy_install in the virtualenv's bin/ folder.
The contents of the bin folder are:
activate activate.csh activate.fish activate_this.py python python2 python2.7
My virtualenv version is 1.11, python is 2.7.5, this is all running on webfaction, for which I think it's commonly known that virtualenv poses some problems? I have a working virtualenv though already installed on the server, so that's strange to me.
I've run into a similar problem running virtualenv and pyvenv, but usually when the --no-pip or --no-setuptools options are passed. You can install pip inside the environment by activating the virtual environment and using easy_install to install pip into the environment:
$> cd path/to/my/env
$> source bin/activate
$> easy_install pip
This is assuming that your environment got easy_install, which from your ls looks like a "no"... But you can get easy_install by installing setuptools. Have you tried regenerating the environment with virtualenv again?

virtualenv not copying the existing python modules

python -c 'import pyscopg2'
this works fine.
Now I create a virtualenv
virtualenv venv
and activate it
source venv/bin/activate
and run the following
python -c 'import pyscopg2'
But it gives error 'ImportError: No module named pyscopg2'
You should create virtual environment using --system-site-packages option to access system package.
According to virtualenv --help:
--no-site-packages Don't give access to the global site-packages dir to
the virtual environment (default)
--system-site-packages
Give access to the global site-packages dir to the
virtual environment

Categories