How to create an empty python virtual environment
Question:
When creating a new environment with conda we get a completely empty virtual environment:
conda create --name=test
conda activate test
conda list
The output of the last command is an empty list, there’s not even pip
installed. I’d like to achieve the same result with python venv
command (or at least have the “minimal” virtual environment with only pip
installed). When I run python -m venv test
the new environment contains all packages available “system-wide”:
python -m venv test
source test/bin/activate
pip freeze
outputs a long list of packages.
According to the documentation the command has --system-site-packages
parameter but it looks like it’s on by default, I can’t find a way to disable it. I’ve also tried using the old virtualenv --clear
parameter but obviously it’s not taken into account.
EDIT:
It turned out to be the environment modules module
command interfering with python modules (https://modules.readthedocs.io/en/latest/). After running module purge
pip freeze
returns empty list.
Answers:
EDIT:
try the following:
$ python3 --version
Python 3.7.4
$ python3 -m venv test_venv
$ source ./test_venv/bin/activate
$ pip list
Package Version
---------- -------
pip 19.0.3
setuptools 40.8.0
You are using pip version 19.0.3, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(test_venv)
$ pip freeze
(test_venv)
If you have virtualenv
installed, to create a ‘fresh’ virtual env without “system wide” pip packages, try the following:
# create new folder:
$ mkdir test_venv
# create virtual env:
$ virtualenv test_venv/
# activate virtual env:
$ source ./test_venv/bin/activate
# list packages in virtual env (test_venv):
$ pip list
Package Version
---------- -------
pip 19.2.3
setuptools 41.2.0
wheel 0.33.6
(test_venv)
A configuration file pyvenv.cfg
should be located within the virtual environment’s root directory when we create a virtual environment with venv
. According to the documentation this file should contain a line with an include-system-site-packages
key and set to false
if venv
previously was run without the --system-site-packages
option.
I wanted to create a virtual environment with a specific python version. This is what I did.
Installled virtualenv
pip install virtualenv
Go to the python version download page (with binary installers). In my case I needed python 3.7, so the latest release with binay installers was version 3.7.9 https://www.python.org/downloads/release/python-379/
Go to that page and downloaded Windows x86-64 embeddable zip file
extracted the zip file and placed the contents inside the project folder.
Then used,
virtualenv --python .python379python.exe env
To create the virtual environment.
If you get any error like virtualenv: The term 'virtualenv' is not recognized as a name of a cmdlet,
uninstall virtualenv
using pip uninstall virtualenv
, close the terminal, open terminal as Administrator / root user and install again.
When creating a new environment with conda we get a completely empty virtual environment:
conda create --name=test
conda activate test
conda list
The output of the last command is an empty list, there’s not even pip
installed. I’d like to achieve the same result with python venv
command (or at least have the “minimal” virtual environment with only pip
installed). When I run python -m venv test
the new environment contains all packages available “system-wide”:
python -m venv test
source test/bin/activate
pip freeze
outputs a long list of packages.
According to the documentation the command has --system-site-packages
parameter but it looks like it’s on by default, I can’t find a way to disable it. I’ve also tried using the old virtualenv --clear
parameter but obviously it’s not taken into account.
EDIT:
It turned out to be the environment modules module
command interfering with python modules (https://modules.readthedocs.io/en/latest/). After running module purge
pip freeze
returns empty list.
EDIT:
try the following:
$ python3 --version
Python 3.7.4
$ python3 -m venv test_venv
$ source ./test_venv/bin/activate
$ pip list
Package Version
---------- -------
pip 19.0.3
setuptools 40.8.0
You are using pip version 19.0.3, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(test_venv)
$ pip freeze
(test_venv)
If you have virtualenv
installed, to create a ‘fresh’ virtual env without “system wide” pip packages, try the following:
# create new folder:
$ mkdir test_venv
# create virtual env:
$ virtualenv test_venv/
# activate virtual env:
$ source ./test_venv/bin/activate
# list packages in virtual env (test_venv):
$ pip list
Package Version
---------- -------
pip 19.2.3
setuptools 41.2.0
wheel 0.33.6
(test_venv)
A configuration file pyvenv.cfg
should be located within the virtual environment’s root directory when we create a virtual environment with venv
. According to the documentation this file should contain a line with an include-system-site-packages
key and set to false
if venv
previously was run without the --system-site-packages
option.
I wanted to create a virtual environment with a specific python version. This is what I did.
Installled virtualenv
pip install virtualenv
Go to the python version download page (with binary installers). In my case I needed python 3.7, so the latest release with binay installers was version 3.7.9 https://www.python.org/downloads/release/python-379/
Go to that page and downloaded Windows x86-64 embeddable zip file
extracted the zip file and placed the contents inside the project folder.
Then used,
virtualenv --python .python379python.exe env
To create the virtual environment.
If you get any error like virtualenv: The term 'virtualenv' is not recognized as a name of a cmdlet,
uninstall virtualenv
using pip uninstall virtualenv
, close the terminal, open terminal as Administrator / root user and install again.