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:
python-dotenv
pip
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
Windows
Activate Virtual Environment:
Mac
Windows
Installing Packages:
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:
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:
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.
Activate Virtual Environment:
Skip this step if you plan to use venv
for your environment and use pipenv
for installing packages.
Installing Packages:
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
PC
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
Accessing Environment Variables:
EXPORT & SET
Python-dotenv
If you're using flask v1.0 or above you don't need to import and call the load_dotenv.
List Available Variables:
MAC
PC
Unsetting an EXPORT or SET Variable:
MAC
PC
Last updated