Virtual Environments

Information I have come across with Virtual Environments, Packages, and Environment Variables

Virtual Environments, Packages, and Environment Variables

I have two different ways listed below for creating Virtual Environments. There are many other options but these are my current favorites. I'm currently looking into pyenv for my python version management and poetry for my package management. My current recommendation is to use venv over pipenv. Please make sure to add your venv folder to your .gitignore if you plan to use version control.

If you are using windows, I personally have run into a lot of issues with Powershell, I would recommend using cmd prompt.

Documentation:

Venv:

The following information is for building a virtual environment via Venv. You can read more about it at the official python documentation listed above.

Create Virtual Environment:

Wherever you run this command it will create a venv folder. If you want the folder to be called something different change the second venv.

Mac

$ python3 -m venv venv

Windows

$ python -m venv venv

Activate Virtual Environment:

Mac

$ source venv/bin/activate
(venv) $ _

Windows

$ venv\Scripts\activate
(venv) $ _

Installing Packages:

(venv) $ pip install package-name

I have sometimes had packages not install correctly because my pip was out of date. I have grown a habbit of updating pip when creating a new virtual environment. This isn't required but it has seemed to help.

python -m pip install --upgrade pip

Requirements File:

If you ever need to regenerate your environment on another machine, you are going to have trouble remembering what packages you had to install, so the generally accepted practice is to write a requirements.txt file in the root folder of your project listing all the dependencies, along with their versions. Producing this list is actually easy:

(venv) $ pip freeze > requirements.txt

The pip freeze command will dump all the packages that are installed on your virtual environment in the correct format for the requirements.txt file. Now, if you need to create the same virtual environment on another machine, instead of installing packages one by one, you can run:

(venv) $ pip install -r requirements.txt

Pipenv:

The following information is for building a virtual environment via Venv. You can read more about it at the official python documentation. https://pipenv.pypa.io/en/latest/ My current recommendation is to use the venv option listed above or a mixture of the two. When using pipenv the environment folder isn't created inside your project directory. Be mindful you can setup venv first and then pipenv will use that venv location for the environment. Also when using pipenv you will get a pipfile and pipfile.lock for your package dependencies management, so there is no need to create a requirements.txt file. Refer to the pipenv documentation for a detailed description on the two files.

Create Virtual Environment:

Skip this step if you plan to use venv for your environment and use pipenv for installing packages.

$ pipenv --three

Activate Virtual Environment:

Skip this step if you plan to use venv for your environment and use pipenv for installing packages.

$ pipenv shell

Installing Packages:

(venv) $ pipenv install package_name

Environment Variables:

Sometimes you need to create environment variables for your project. You can easily create them for your current terminal session with a simple EXPORT or SET command. The downfall is the variable isn't remembered across terminal sessions. A more popular way is using a package called python-dotenv, which gives you the ability to store these variables inside a file. If you plan on using version control with the python-dotenv method, please remember to hide your environment file in a .gitignore file

Be mindful you can set variables globally or specific to your virtual environment. Make sure you're inside your virtual environment before running the export commands.

When you host applications on heroku your environment variables are called config variables. Luckily the methods for accessing your environment variables are the same.

If any of these commands don't work for you please visit https://ss64.com/ for detailed information on your operating system.

EXPORT & SET:

MAC

(venv) $ export VAR_NAME=secret

PC

(venv) $ set VAR_NAME=secret

Python-dotenv:

Create a file in your root directory named .env

Are you using version control? Add this file to your .gitignore file

It is a common convention to use ALL_CAPS_WITH_SNAKE_CASE_FOR_VARIABLE_NAMES. There is also no need for spaces before and after the = sign along with no need for quotes for strings.

Env file example

SECRET_PASSWORD=password123
API_KEY=1234
DATABASE_URI=postgresql://user:password@postgresserver/db

Accessing Environment Variables:

EXPORT & SET

import os
print(os.getenv('VAR_NAME'))
print(os.environ.get('VAR_NAME'))
print(os.environ['VAR_NAME'])

Python-dotenv

If you're using flask v1.0 or above you don't need to import and call the load_dotenv.

import os
from dotenv import load_dotenv
load_dotenv()

print(os.getenv('VAR_NAME'))
print(os.environ.get('VAR_NAME'))
print(os.environ['VAR_NAME'])

List Available Variables:

MAC

(venv) $ printenv

PC

(venv) $ SET

Unsetting an EXPORT or SET Variable:

MAC

(venv) $ unset VAR_NAME

PC

(venv) $ SET VAR_NAME=

Last updated