Stonecraft
Stonecraft

Reputation: 814

How can I create a python virtual environment that only includes one version of python/pip?

I had been under the impression that if I created a python environment with a specific python version, it would use just that version, but it seems I was mistaken:

$ /usr/bin/python3 -m venv pyenv
$ ls pyenv
$ ls pyenv/bin
activate      activate.fish  easy_install      pip   pip3.8  python3
activate.csh  Activate.ps1   easy_install-3.8  pip3  python

Is there any reason why older versions of python need to exist alongside the current one in my venv? And can I specify not to, or should I just manually delete the python2/pip?

Upvotes: 0

Views: 266

Answers (1)

alani
alani

Reputation: 13079

In your virtual environment, all the python* executables will point to the same version. You can see this from the symbolic links:

$ /usr/bin/python3 -m venv pyenv

$ cd pyenv/bin

$ ls -l python*
lrwxrwxrwx 1 [user] [group]  7 May 28 05:49 python -> python3
lrwxrwxrwx 1 [user] [group] 16 May 28 05:49 python3 -> /usr/bin/python3

Similarly all the pip executables will be copies of the same file:

$ ls -l pip*
-rwxr-xr-x 1 [user] [group] 212 May 28 05:49 pip
-rwxr-xr-x 1 [user] [group] 212 May 28 05:49 pip3
-rwxr-xr-x 1 [user] [group] 212 May 28 05:49 pip3.6

$ diff3 pip*
[no output]

This means, for example, that once you have run the activate script so that this bin directory is in your PATH, you will find the python in your virtual environment regardless of whether you type python or python3.

The same is true of python scripts which you invoke with #!/usr/bin/env python or #!/usr/bin/env python3 for example.

Only if you explicitly invoke a different version of python (for example python2) or give a full path to the python executable (for example /usr/bin/python) might you find another version located elsewhere on your system.

Upvotes: 2

Related Questions