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.
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:
Mac
$ python3 -m venv venvWindows
$ python -m venv venvActivate Virtual Environment:
Mac
$ source venv/bin/activate
(venv) $ _Windows
$ venv\Scripts\activate
(venv) $ _Installing Packages:
(venv) $ pip install package-nameRequirements 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.txtThe 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.txtPipenv:
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 --threeActivate Virtual Environment:
Skip this step if you plan to use venv for your environment and use pipenv for installing packages.
$ pipenv shellInstalling Packages:
(venv) $ pipenv install package_nameEnvironment 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
EXPORT & SET:
MAC
(venv) $ export VAR_NAME=secretPC
(venv) $ set VAR_NAME=secretPython-dotenv:
Create a file in your root directory named .env
Env file example
SECRET_PASSWORD=password123
API_KEY=1234
DATABASE_URI=postgresql://user:password@postgresserver/dbAccessing Environment Variables:
EXPORT & SET
import os
print(os.getenv('VAR_NAME'))
print(os.environ.get('VAR_NAME'))
print(os.environ['VAR_NAME'])Python-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) $ printenvPC
(venv) $ SETUnsetting an EXPORT or SET Variable:
MAC
(venv) $ unset VAR_NAMEPC
(venv) $ SET VAR_NAME=Last updated
Was this helpful?