Environment
December 3, 2023About 1 min
environ-python
# load environment file
environ.Env.read_env(str(ENV_FILENAME))
# read environment variable
env = environ.Env()
# Load the values in the env file if it exists
env('ENV_FILENAME', default='local.env')
USE_PATH_FOR_GDAL_PYTHON=YES
python-dotenv
pip install python-dotenv
from dotenv import load_dotenv
import os
load_dotenv() # This line brings all environment variables from .env into os.environ
print(os.environ['VONAGE_API'])
Virtual Environment
Virtual Python management tools
Tool | |
---|---|
Pipenv | Pipenv is a Python virtualenv management tool that supports a multitude of systems and nicely bridges the gaps between pip, python (using system python, pyenv or asdf) and virtualenv. Linux, macOS, and Windows are all first-class citizens in pipenv. |
Venv | The venv module supports creating lightweight “virtual environments”, each with their own independent set of Python packages installed in their site directories. A virtual environment is created on top of an existing Python installation, known as the virtual environment’s “base” Python, and may optionally be isolated from the packages in the base environment, so only those explicitly installed in the virtual environment are available. |
Conda | Conda is an open source package management system and environment management system for installing multiple versions of software packages and their dependencies and switching easily between them. It works on Linux, OS X and Windows, and was created for Python programs but can package and distribute any software. |
pipenv
venv
When a Python interpreter is running from a virtual environment, sys.prefix
and sys.exec_prefix
point to the directories of the virtual environment, whereas sys.base_prefix
and sys.base_exec_prefix
point to those of the base Python used to create the environment. It is sufficient to check sys.prefix != sys.base_prefix
to determine if the current interpreter is running from a virtual environment.
Create virtual environment
python -m venv c:\path\to\myenv
Active the virtual environmentc:\path\to\myenv\Scripts\activate.bat
Deactive the virtual environment
You can deactivate a virtual environment by c:\path\to\myenv\Scripts\activate.bat
in your shell.