OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
os.environ
in Python is a mapping object that represents the user’s environmental variables. It returns a dictionary having user’s environmental variable as key and their values as value.
os.environ
behaves like a python dictionary, so all the common dictionary operations like get and set can be performed. We can also modify os.environ
but any changes will be effective only for the current process where it was assigned and it will not change the value permanently.
Syntax: os.environ
Parameter: It is a non-callable object. Hence, no parameter is required
Return Type: This returns a dictionary representing the user’s environmental variables
Code #1: Use of os.environ to get access of environment variables
import
os
import
pprint
env_var
=
os.environ
print
(
"User's Environment variable:"
)
pprint.pprint(
dict
(env_var), width
=
1
)
Output:
{'CLUTTER_IM_MODULE': 'xim', 'COLORTERM': 'truecolor', 'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1000/bus', 'DESKTOP_SESSION': 'ubuntu', 'DISPLAY': ':0', 'GDMSESSION': 'ubuntu', 'GJS_DEBUG_OUTPUT': 'stderr', 'GJS_DEBUG_TOPICS': 'JS ' 'ERROR;JS ' 'LOG', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'GNOME_SHELL_SESSION_MODE': 'ubuntu', 'GTK_IM_MODULE': 'ibus', 'HOME': '/home/ihritik', 'IM_CONFIG_PHASE': '2', 'JAVA_HOME': '/opt/jdk-10.0.1', 'JOURNAL_STREAM': '9:28586', 'JRE_HOME': '/opt/jdk-10.0.1/jre', 'LANG': 'en_IN', 'LANGUAGE': 'en_IN:en', 'LESSCLOSE': '/usr/bin/lesspipe ' '%s ' '%s', 'LESSOPEN': '| ' '/usr/bin/lesspipe ' '%s', 'LOGNAME': 'ihritik', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: /usr/local/games:/snap/bin:/usr/local/java/jdk-10.0.1/bin: /usr/local/java/jdk-10.0.1/jre/bin:/opt/jdk-10.0.1/bin:/opt/jdk-10.0.1/jre/bin', 'PWD': '/home/ihritik', 'QT4_IM_MODULE': 'xim', 'QT_IM_MODULE': 'ibus', 'SESSION_MANAGER': 'local/hritik:@/tmp/.ICE-unix/1127, unix/hritik:/tmp/.ICE-unix/1127', 'SHELL': '/bin/bash', 'SHLVL': '2', 'SSH_AUTH_SOCK': '/run/user/1000/keyring/ssh', 'TERM': 'xterm-256color', 'TEXTDOMAIN': 'im-config', 'TEXTDOMAINDIR': '/usr/share/locale/', 'USER': 'ihritik', 'USERNAME': 'ihritik', 'VTE_VERSION': '4804', 'WAYLAND_DISPLAY': 'wayland-0', 'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/etc/xdg', 'XDG_CURRENT_DESKTOP': 'ubuntu:GNOME', 'XDG_MENU_PREFIX': 'gnome-', 'XDG_RUNTIME_DIR': '/run/user/1000', 'XDG_SEAT': 'seat0', 'XDG_SESSION_DESKTOP': 'ubuntu', 'XDG_SESSION_ID': '2', 'XDG_SESSION_TYPE': 'wayland', 'XDG_VTNR': '2', 'XMODIFIERS': '@im=ibus', '_': '/usr/bin/python3'}
Code #2: Accessing a particular environment variable
import
os
home
=
os.environ[
'HOME'
]
print
(
"HOME:"
, home)
java_home
=
os.environ.get(
'JAVA_HOME'
)
print
(
"JAVA_HOME:"
, java_home)
Output:
HOME: /home/ihritik JAVA_HOME: /opt/jdk-10.0.1
Code #3: Modifying a environment variable
import
os
print
(
"JAVA_HOME:"
, os.environ[
'JAVA_HOME'
])
os.environ[
'JAVA_HOME'
]
=
'/home / ihritik / jdk-10.0.1'
print
(
"Modified JAVA_HOME:"
, os.environ[
'JAVA_HOME'
])
Output:
JAVA_HOME: /opt/jdk-10.0.1 Modified JAVA_HOME: /home/ihritik/jdk-10.0.1
Code #4: Adding a new environment variable
import
os
os.environ[
'GeeksForGeeks'
]
=
'www.geeksforgeeks.org'
print
(
"GeeksForGeeks:"
, os.environ[
'GeeksForGeeks'
])
Output:
GeeksForGeeks: www.geeksforgeeks.org
Code #5: Accessing a environment variable which does not exists
import
os
print
(
"MY_HOME:"
, os.environ[
'MY_HOME'
]
Output:
Traceback (most recent call last): File "osenviron.py", line 8, in print("MY_HOME:", os.environ['MY_HOME']) File "/usr/lib/python3.6/os.py", line 669, in __getitem__ raise KeyError(key) from None KeyError: 'MY_HOME'
Code #6: Handling error while Accessing a environment variable which does not exists
import
os
print
(
"MY_HOME:"
, os.environ.get(
'MY_HOME'
,
"Environment variable does not exist"
))
try
:
print
(
"MY_HOME:"
, os.environ[
'MY_HOME'
])
except
KeyError:
print
(
"Environment variable does not exist"
)
Output:
MY_HOME: Environment variable does not exist Environment variable does not exist
Environment variables provide a great way to configure your Python application, eliminating the need to edit your source code when the configuration changes. Common configuration items that are often passed to application through environment variables are third-party API keys, network ports, database servers, and any custom options that your application may need to work properly.
In this article I’m going to share some of the techniques and tools available in Python to work with environment variables.
How to access environment variables from Python
Using the os.environ dictionary
In Python, the os.environ
dictionary contains all the environment variables. The simplest way to retrieve a variable from inside your application is to use the standard dictionary syntax. For example, this is how you can access an environment variable named USER
:
>>> import os
>>> user = os.environ['USER']
>>> user
'miguel'
Using this method, if you try to import an environment variable that doesn’t exist Python will raise a KeyError
exception:
>>> database_url = os.environ['DATABASE_URL']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/miguel/.pyenv/versions/3.8.6/lib/python3.8/os.py", line 675, in __getitem__
raise KeyError(key) from None
KeyError: 'DATABASE_URL'
Using os.environ.get()
Getting the KeyError
is a good idea for environment variables that your program requires, but there are situations where you may want to allow some variables to be optional. To avoid the error you can use the dictionary’s get()
method, which returns None
when the requested key does not exist in the dictionary:
>>> database_url = os.environ.get('DATABASE_URL')
Adding a default value if the variable is not defined
If you’d like to provide a default value for the missing variable that is not None
, you can add it as a second argument:
>>> database_url = os.environ.get('DATABASE_URL', 'sqlite:///')
>>> database_url
'sqlite:///'
Using the os.getenv() function
Python also provides the os.getenv()
function to access environment variables. This function works in a very similar way to the os.environ.get()
method. Here is how to access a variable with it:
>>> user = os.getenv('USER')
>>> user
'miguel'
This function does not raise an error for missing variables, it returns None
just like os.environ.get()
. And it also accepts a second argument with a custom default value:
>>> database_url = os.getenv('DATABASE_URL', 'sqlite://')
>>> database_url
'sqlite://'
Is os.getenv()
better than os.environ
? That is really up to you. I personally prefer to use the os.environ
dictionary, since it gives me the option of halting the program with a KeyError
if a required variable is missing.
How to set environment variables
In this section I’m going to give you a quick summary of how to set environment variables in a terminal or command prompt window. If you want to know all the possible ways to set environment variables, my colleague Dominik Kundel has written a very detailed blog post on this subject titled How to Set Environment Variables.
Unix and MacOS
There are two basic ways to set an environment variable from a bash or zsh terminal session. One is using the export
keyword:
A variable that is set in this way will be passed on to any programs or scripts that you start from this terminal session. Keep in mind that environment variables are not saved anywhere outside of the context of the shell session, so they will go away when you close the terminal session.
An alternative way to define an environment variable is to set it in the same line where the target application is executed:
DEBUG=true python my_cool_application.py
This second form has the benefit that the variable is only set in the environment space of the intended application.
Microsoft Windows
If you are using Windows you have a few options. If you are interested in setting environment variables via the control panel, see the article linked above.
If you are in a command prompt window, you can set an environment variable using the set
command:
Like in the Unix case, the variable is not stored or remembered beyond the current session.
If you are using the newer PowerShell console, the syntax for setting environment variables is completely different:
Finally, if you are using a Unix compatibility layer such as WSL or Cygwin, then you must go to the Unix section above and use any of the methods listed there for bash or zsh.
Using .env files
Are you confused by all the different ways to set environment variables? I personally find it inconvenient that each platform or shell requires a different procedure.
In my opinion, a better way to manage your environment variables is to store them in a .env (pronounced dot env) file. A .env file is a text file in which the variables are defined, one per line. The format of a .env file is exactly the same under all operating systems, so .env files make working with environment variables uniform across all platforms. And as if this isn’t enough, having your environment variables written in a file that is automatically imported by Python means that you don’t have to manually set them every time you start a new shell.
Here is a short .env file example with two variables:
DEBUG=true
DATABASE_URL=sqlite:///mydb.sqlite
You can create a .env file in the root directory of each of your projects, and that way you can keep all the variables that are needed by each project neatly organized!
The python-dotenv package allows a Python application to import variables defined in a .env file into the environment. You can install python-dotenv
in your virtual environment using pip
:
pip install python-dotenv
Below you can see how to import a .env file into a Python application:
>>> from dotenv import load_dotenv
>>> load_dotenv()
The load_dotenv()
function will look for a file named .env in the current directory and will add all the variable definitions in it to the os.environ
dictionary. If a .env file is not found in the current directory, then the parent directory is searched for it. The search keeps going up the directory hierarchy until a .env file is found or the top-level directory is reached.
If you want to prevent python-dotenv from searching for a .env file through your directories, you can pass an explicit path to your file as an argument to load_dotenv()
:
>>> from dotenv import load_dotenv
>>> load_dotenv('/home/miguel/my_project/.env')
There are some additional arguments that you can use when you call the load_dotenv()
function. If you want to learn about them consult the documentation.
Once the .env file is imported, you can access environment variables using any of the methods shown above.
A note about .env file security
In many cases you will be adding environment variables that contain sensitive information to your .env files, like passwords or API keys. For that reason, in general you do not want to add these files to your project’s source control repository.
The standard practice is to add an exception for files with this name, so that they are not committed to source control by mistake. For git, you can add a line with the name of the file to the .gitignore file in the root of your repository.
But if you cannot commit the .env file, how do you tell users of the project which variables need to be set? For this you can add an example .env file to your repository with a name such as .env.example, that contains the list of variables that the project requires, but without including their values. This serves as guidance for users of your project, without giving away sensitive information.
Conclusion
I hope this article was useful to help you understand how to use environment variables to configure your Python projects.
Do you have any other ways to work with environment variables? I’d love to know!
Miguel Grinberg is a Python Developer for Technical Content at Twilio. Reach out to him at mgrinberg [at] twilio [dot] com if you have a cool Python project you’d like to share on this blog!
Модуль ОС в Python предоставляет функции для взаимодействия с операционной системой. ОС поставляется под стандартные служебные модули Python. Этот модуль предоставляет портативный способ использования функциональных возможностей, зависящих от операционной системы.
os.environ
в Python — это объект отображения, который представляет переменные среды пользователя. Он возвращает словарь, имеющий переменную окружения пользователя в качестве ключа и их значения в качестве значения.
os.environ
ведет себя как словарь python, поэтому можно выполнять все обычные словарные операции, такие как get и set. Мы также можем изменить os.environ
но любые изменения будут эффективны только для текущего процесса, в котором он был назначен, и не будут изменять значение навсегда.
Syntax: os.environ
Parameter: It is a non-callable object. Hence, no parameter is required
Return Type: This returns a dictionary representing the user’s environmental variables
Код № 1: Использование os.environ для получения доступа к переменным среды
import
os
import
pprint
env_var
=
os.environ
print
(
"User's Environment variable:"
)
pprint.pprint(
dict
(env_var), width
=
1
)
Выход:
{'CLUTTER_IM_MODULE': 'xim', 'COLORTERM': 'truecolor', 'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1000/bus', 'DESKTOP_SESSION': 'ubuntu', 'DISPLAY': ':0', 'GDMSESSION': 'ubuntu', 'GJS_DEBUG_OUTPUT': 'stderr', 'GJS_DEBUG_TOPICS': 'JS ' 'ERROR;JS ' 'LOG', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'GNOME_SHELL_SESSION_MODE': 'ubuntu', 'GTK_IM_MODULE': 'ibus', 'HOME': '/home/ihritik', 'IM_CONFIG_PHASE': '2', 'JAVA_HOME': '/opt/jdk-10.0.1', 'JOURNAL_STREAM': '9:28586', 'JRE_HOME': '/opt/jdk-10.0.1/jre', 'LANG': 'en_IN', 'LANGUAGE': 'en_IN:en', 'LESSCLOSE': '/usr/bin/lesspipe ' '%s ' '%s', 'LESSOPEN': '| ' '/usr/bin/lesspipe ' '%s', 'LOGNAME': 'ihritik', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: /usr/local/games:/snap/bin:/usr/local/java/jdk-10.0.1/bin: /usr/local/java/jdk-10.0.1/jre/bin:/opt/jdk-10.0.1/bin:/opt/jdk-10.0.1/jre/bin', 'PWD': '/home/ihritik', 'QT4_IM_MODULE': 'xim', 'QT_IM_MODULE': 'ibus', 'SESSION_MANAGER': 'local/hritik:@/tmp/.ICE-unix/1127, unix/hritik:/tmp/.ICE-unix/1127', 'SHELL': '/bin/bash', 'SHLVL': '2', 'SSH_AUTH_SOCK': '/run/user/1000/keyring/ssh', 'TERM': 'xterm-256color', 'TEXTDOMAIN': 'im-config', 'TEXTDOMAINDIR': '/usr/share/locale/', 'USER': 'ihritik', 'USERNAME': 'ihritik', 'VTE_VERSION': '4804', 'WAYLAND_DISPLAY': 'wayland-0', 'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/etc/xdg', 'XDG_CURRENT_DESKTOP': 'ubuntu:GNOME', 'XDG_MENU_PREFIX': 'gnome-', 'XDG_RUNTIME_DIR': '/run/user/1000', 'XDG_SEAT': 'seat0', 'XDG_SESSION_DESKTOP': 'ubuntu', 'XDG_SESSION_ID': '2', 'XDG_SESSION_TYPE': 'wayland', 'XDG_VTNR': '2', 'XMODIFIERS': '@im=ibus', '_': '/usr/bin/python3'}
Код № 2: Доступ к определенной переменной среды
import
os
home
=
os.environ[
'HOME'
]
print
(
"HOME:"
, home)
java_home
=
os.environ.get(
'JAVA_HOME'
)
print
(
"JAVA_HOME:"
, java_home)
Выход:
HOME: /home/ihritik JAVA_HOME: /opt/jdk-10.0.1
Код № 3: Изменение переменной среды
import
os
print
(
"JAVA_HOME:"
, os.environ[
'JAVA_HOME'
)
os.environ[
'JAVA_HOME'
]
=
'/home / ihritik / jdk-10.0.1'
print
(
"Modified JAVA_HOME:"
, os.environ[
'JAVA_HOME'
])
Выход:
JAVA_HOME: /opt/jdk-10.0.1 Modified JAVA_HOME: /home/ihritik/jdk-10.0.1
Код № 4: Добавление новой переменной среды
import
os
os.environ[
'GeeksForGeeks'
]
=
'www.geeksforgeeks.org'
print
(
"GeeksForGeeks:"
, os.environ[
'GeeksForGeeks'
])
Выход:
GeeksForGeeks: www.geeksforgeeks.org
Код № 5: Доступ к переменной среды, которая не существует
import
os
print
(
"MY_HOME:"
, os.environ[
'MY_HOME'
]
Выход:
Traceback (most recent call last): File "osenviron.py", line 8, in print("MY_HOME:", os.environ['MY_HOME']) File "/usr/lib/python3.6/os.py", line 669, in __getitem__ raise KeyError(key) from None KeyError: 'MY_HOME'
Код № 6: Обработка ошибки при доступе к переменной среды, которая не существует
import
os
print
(
"MY_HOME:"
, os.environ.get(
'MY_HOME'
,
"Environment variable does not exist"
))
try
:
print
(
"MY_HOME:"
, os.environ[
'MY_HOME'
])
except
KeyError:
print
(
"Environment variable does not exist"
)
Выход:
MY_HOME: Environment variable does not exist Environment variable does not exist
Рекомендуемые посты:
- Объектно-ориентированное программирование на Python | Набор 2 (скрытие данных и печать объектов)
- Объектно-ориентированное программирование на Python | Набор 1 (класс, объект и члены)
- Python | os.supports_bytes_environ объект
- Python | метод object ()
- Python | os.sysconf_names объект
- Python | os.supports_follow_symlinks объект
- Python | объект os.confstr_names
- Python | os.supports_bytes_environ объект
- Python | объект os.supports_dir_fd
- Python | объект os.supports_fd
- Сравнение объектов Python: is vs ==
- pickle — сериализация объектов Python
- Python | Проверьте, является ли данный объект списком или нет
- Python | os.path.supports_unicode_filenames объект
- Как создать список объектов в классе Python
Python | объект os.environ
0.00 (0%) 0 votes