Using a virtual environment allows you to avoid installing Python modules globally which could break system tools or other projects. ๐ฅ
If you are using Python 3.3 or newer the "venv" module is the preferred way to create and manage virtual environments.
You can check what version of Python you have via:
python --version
If the version is older than 3.3 you can install the "virtualenv" module:
pip install virtualenv
Creating the virtual environment is easy and can be done via:
# venv
python3 -m venv env
# virtualenv
virtualenv env
Before you can start installing modules etc, you need to activate the environment. Activating the virtual environment will put the environment specific "python" and "pip" executables into your shell's path.
source env/bin/activate
You can check if the path has been updated via echoing it.
echo $PATH
You should see the virtualenv directory being listed at the front. ๐
Once you are done with the virtual environment you can simple deactivate it.
deactivate
Simples. ๐
I personally wouldn't recommend it.
I'd recommend putting the modules you need in a "requirements.txt" file, creating the virtual environment on the deployment server and then installing the modules needed via:
pip install -r requirements.txt
Don't forget to add the virtual environment directory to your ".gitignore" file. ๐
Like me work? I post about a variety of topics, if you would like to see more please like and follow me. Also I love coffee.