For Debian based GNU/Linux
A lot of built in programs in Linux, OSX or POSIX compilant often rely on the system python, the executable to which, is in /usr/bin/python
. Because a lot depend on it, it is not a good idea to mess with it, change it's version, or add new libraries into it.
The better way is to have the dependencies contained within it's own environment, so system python changes (due to upgrade) and other environment's changes don't affect the current project.
the steps below mostly compatible with Debian based GNU/Linux. however, it may possible on OSX and other linux distributions too. The main idea is just the same.
- Install virtualenv to bash or terminal(use pip3 for python3):
pip install virtualenv
or
sudo pip install virtualenv
or
sudo apt install virtualenv
- Make a directory wherever suits you. This directory will be used for several virtual environments. for example in terminal:
mkdir python_enviroments
cd python_enviroments
then, call virtualenv
command and give your environment name to it. for example:
virtualenv env1
it will create a folder named env1 with all of the python tools in it, but it's not yet usable.
- To start using it, you need to be on the environment folder (env1) to activate the virtual environment and execute the
activate
file:
cd env1
source bin/activate
this will activate your python virtual envinroment. you can distinguish it by seeing your environment folder in parenthesis before the prompt. it will look similar to:
(env1) user@computer ~/python_enviroments/ $
this will separate your python dependencies and installed packages from main system.
- Now, you can install your required packages with
pip install
command.
(optional)after installing the packages, save the dependencies into a text file by typing:
pip freeze requirements.txt
- After finished using the environment, you can exit the virtual environment by typing
deactivate
venv module, part of the standard Python 3 library
- If not yet included in your python3, use this line in terminal.
sudo apt-get install -y python3-venv
- then, create folder for all the envs:
mkdir environments
cd environments
- Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:
python3 -m venv my_env
- after it's done, you can activate it using:
source my_env/bin/activate
- (optional)to edit and include it on
vscode
just type it the current terminal
code .
- Go to the parent folder in which
venv
is there through command prompt. - Type
code .
and Enter. [Working on both windows and linux for me.] - That should also show the virtual environments present in that folder.