More on Python Virtual Environments

My Original Article on Virtual Environments

In my previous article, I talked about two ways to create Python virtual environments using mkvirtualenv and pyenv. What I wasn’t aware of at the time is that in Python 3.3 and above a native ability to create a virtual environment was added to python.

REMINDER: The Reason we want to make virtual environments is to isolate our project dependency and manage dependency versions, kinda like what a package.json does for us in Javascript, a Gemfile in Ruby, cargo.toml in rust, composer.json in PHP, you get the idea.

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

This will create all the folders in that file, so let’s say I specified a folder in my current directory called “venv” as the target to create the venv. I can easily activate it like so.

source ./venv/bin/activate
  • use the command “deactivate” to turn off venv

You’ll then see it active in your terminal. Although remembering all that may be tricky. Below is some bash function you may want to add to ~/.bashrc or ~/.zshrc that’ll make this process easier.

function newpyenv {
mkdir venv
python -m venv ./venv
}
function usepyenv {
source ./venv/bin/activate
}

If you add this to bshrc/zshrc you can then restart your terminal and these commands will be active.

  • newpyenv: Will create venv folder in your current folder and create a venv in that folder
  • 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.

You could also use this on a per-project basis, whatever you prefer. Hopefully, these bash functions make your life easier!

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why is Communication a Top soft skill for engineers?

Ecosystem AMA — March 23rd, 2022

READ/DOWNLOAD%( The Linux Command Line: A Complete Introduction FULL BOOK PDF & FULL AUDIOBOOK

• Slow site servers that can’t cope well with a sudden increase in traffic, which leads to problems…

A step towards reinventing cyber security for the ‘new world’

WhatsApp rolls out emoji reactions, bigger files, and massive groups : Gadget Game News

x64 SLAE — Assignment 3: Egg Hunter

ZenHub’s move to Kubernetes

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
Alex Merced Coder

Alex Merced Coder

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

More from Medium

Backend with Python:

Tired of waiting? Upload your pull request on TestPyPI in the meantime

Affordable Speech-To-Text with Python

Write a Python program using Anaconda Prompt or terminal