More on Python Virtual Environments

How to Use the Native Venv Module

Create a New Virtual Environment

You may need to write python3, just make sure you’re using the command that is python 3.3 or above. You can always run python — version or python3 — version to double-check. If you use pyenv you can manage what version of python the python command uses at any time.

python -m venv ~/path/to/where/venv/will/be/created
source ./venv/bin/activate
function newpyenv {
mkdir venv
python -m venv ./venv
}
function usepyenv {
source ./venv/bin/activate
}
  • usepyenv: if you in a folder where the above command was run (the parent folder to the venv folder) run this command to activate the venv
  • deactivate: this is always the command to turn off an active virtual environment

Example

Let’s say I create a “Flask” folder for creating Flask applications, I can create a virtual environment to share with all my flask apps by running “newpyenv” which I can then activate by running “usepyenv” in my Flask folder before jumping into one of my projects.

Alex Merced is a Full Stack Developer, learn more about his work at AlexMercedCoder.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store