Virtual Environments
Information I have come across with Virtual Environments, Packages, and Environment Variables
Last updated
Was this helpful?
Information I have come across with Virtual Environments, Packages, and Environment Variables
Last updated
Was this helpful?
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.
venv
pipenv
python-dotenv
pip
The following information is for building a virtual environment via Venv. You can read more about it at the official python documentation listed above.
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:
Skip this step if you plan to use venv
for your environment and use pipenv
for installing packages.
Skip this step if you plan to use venv
for your environment and use pipenv
for installing packages.
Create a file in your root directory named .env
The following information is for building a virtual environment via Venv. You can read more about it at the official python documentation.
My current recommendation is to use the 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 for a detailed description on the two files.
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 , 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
When you host applications on 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 for detailed information on your operating system.