Default venv
that comes with new versions of python3 is a subset of the more complete virtualenv
. So it is recommended to use the more complete virtualenv
.
- Install virtual environment
$ sudo apt install python3-virtualenv
- Create a new folder to be used by the new environment
# It is recommended to create an overal folder ~/.virtualenv and then create environment inside this folder.
$ mkdir ~/.virtualenv
- Enter to the environment folder
$ cd ~/.virtualenv
- Create a new environment in the current folder
$ virtualenv myEnv
$ virtualenv -p /usr/bin/python2.7 py27_env
- Created structure
# python3 myEnv structure
$ tree -L 3
.
└── myEnv
├── bin
│ ├── activate
│ ├── activate.csh
│ ├── activate.fish
│ ├── activate.nu
│ ├── activate.ps1
│ ├── activate_this.py
│ ├── deactivate.nu
│ ├── pip
│ ├── pip3
│ ├── pip-3.10
│ ├── pip3.10
│ ├── python -> /usr/bin/python3
│ ├── python3 -> python
│ ├── python3.10 -> python
│ ├── wheel
│ ├── wheel3
│ ├── wheel-3.10
│ └── wheel3.10
├── lib
│ └── python3.10
└── pyvenv.cfg
- Activate virtualenv
$ cd ~/.virtualenv
$ source myEnv/bin/activate
(myEenv) $
- Review and install packages
# check current python version
(myEnv) $ which python
(myEnv) $ python --version
# verify installed packages
(myEnv) $ pip list
# install new packages
(myEnv) $ pip install numpy scipy matplotlib
# save current installed packages to requirements.txt
(myEnv) $ pip freeze --local > requirements.txt
# later on another environemnt, install requirements
(newEnv) $ pip install -r requirements.txt
- Deactivate current venv
(myEnv) $ deactivate
- Delete environment
# simply delete the folder created by the environemnt
$ rm -rf myEnv
Note: Do not create or put your project folder and files inside the virtualenv folder. It should be used only to manage the working version and packages.
- Install virtual environment
> pip install virtualenv
- Create a new folder to be used by the virtual environment
> cd C:\Users\<username>
> mkdir virtualenv
- Enter to the environment folder
> cd virtualenv
- Create a new environment in the current folder
> python -m venv myEnv
- Activate virtualenv
> myEnv\Scripts\activate
(myEenv) >
- Review and install packages
rem check current python version
(myEnv) > python --version
rem verify installed packages
(myEnv) > pip list
rem install new packages
(myEnv) > pip install numpy scipy matplotlib
rem save current installed packages to requirements.txt
(myEnv) > pip freeze --local > requirements.txt
rem later on another environemnt, install requirements
(newEnv) > pip install -r requirements.txt
- Deactivate current venv
(myEnv) > deactivate