Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an empty python virtual environment

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.

like image 887
mjarosie Avatar asked Sep 18 '19 20:09

mjarosie


People also ask

How do you create an empty Python environment?

To create an environment that is absolutely empty, without python and/or any other default package, just make a new folder in envs directory in your Anaconda installation (Anaconda3 in this example):.


2 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) 
like image 66
Huan Avatar answered Sep 30 '22 17:09

Huan


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.

like image 40
damb Avatar answered Sep 30 '22 16:09

damb