Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm not sure how many of these ingredients matter, but I try running a bash script I wrote a long time ago that creates a virtualenv and activates it. I changed it to use python3 as the python version. However, this seems to break the virtualenv and tries to pip install into the global site-packages.

How do I get pip from a python3 virtualenv to install into that virtualenv?

PYTHON=${PYTHON:-python3}
VENV_DIR="bld_tests_env"
$PYTHON -m virtualenv --python=$PYTHON $VENV_DIR
set -x
source $VENV_DIR/bin/activate
which -a python
which -a pip
ls -l `which pip`
pip --version

gives me the following output

+ which -a python
<code path>/staging/bld_tests_env/bin/python
/usr/local/bin/python
/usr/bin/python
+ which -a pip
<code path>/staging/bld_tests_env/bin/pip
/usr/local/bin/pip
+ ls -l `which pip`
<code path>/staging/bld_tests_env/bin/pip
+ pip --version
pip 20.1 from /usr/local/lib/python3.5/site-packages/pip (python 3.5)

Can anybody explain to me why invoking pip after activating the virtualenv still references the global installation?

Are you executing the script with sudo privileges? I'm not entirely sure about this case, but virtual environments get a bit weird when it comes to sudo... – Nicolas Forstner May 15, 2020 at 21:34 @NicolasForstner which -a show proper paths to python and pip — the 1st ones are in the virtual env. – phd May 15, 2020 at 21:50 It really looks like I'm executing the right pip executable, but that pip executable doesn't have the right PYTHONPATH! I hacked my pip script in the virtualenv and added some lines to the main function from pip import _internal if __name__ == '__main__': print(_internal.cli.main.__file__) print(sys.argv) and it outputs "/usr/local/lib/python3.5/site-packages/pip/_internal/cli/main.py" and "['<code path>/staging/bld_tests_env/bin/pip', 'install', '--upgrade', 'pip']" – xaviersjs May 15, 2020 at 22:01

I don't have an explanation for why it's so broken with virtualenv, but I did learn that venv is a built-in module since python 3.3 https://docs.python.org/3/library/venv.html

If I switch from

$PYTHON -m virtualenv --python=$PYTHON $VENV_DIR
$PYTHON -m venv $VENV_DIR

then everything seems to work as expected.

My guess is that virtualenv is not properly supported on python3 and nobody notices because nobody bothers to use the pip package for it.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.