Environment Variables in Python
Last Updated :
14 Apr, 2025
Environment variables are key-value pairs that live in our system's environment. Python reads some of these variables at startup to determine how to behave, for example, where to look for modules or whether to enter interactive mode.
If there’s ever a conflict between an environment variable and a command-line argument, the command-line argument is given priority. Let’s explore some of the most frequently used environment variables in Python:
PYTHONPATH
Adds user-defined directories to Python’s module search path (sys.path).
Use Case: Helps Python find modules that aren't installed in standard locations.
Example:
export PYTHONPATH="/home/user/mymodules"
PYTHONHOME
Sets the default location for Python’s standard libraries.
Use Case: Useful when embedding Python or using a custom Python distribution.
Example:
prefix/lib/pythonX.X
exec_prefix/lib/pythonX.X
PYTHONSTARTUP
Whenever the python interpreted is first initialized Python looks for a readable file with the name .pythonrc.py in Unix and executes the commands inside it. The path to the same file is stored in the PYTHONSTARTUP variable. These files are responsible for setting up the PYTHONPATH.
PYTHONINSPECT
Forces Python to enter interactive mode after running a script. It can also make changes to the Python code and force it to enter the inspect mode on program termination. It is equivalent to using the -i command-line option.
Use Case: Helpful during debugging or quick testing.
PYTHONCASEOK (Windows only)
This environment variable is used to ignore all import statements while calling the Python interpreter. It is used to find the first case-insensitive match in an import statement.
Use Case: Useful when working with files on case-sensitive and case-insensitive platforms.
PYTHONVERBOSE
If this variable is set to an empty string, it prints a message every time a module is initialized showing the location of the file or the module from where it has been loaded. It also generates information on module cleanup at the termination of the python program.
Use Case: Helpful for debugging import-related issues.
The above variables are the primary environment variables that are frequently used.
Ways to Run Python Code
There are 3 standard ways of using Python, namely:
- Using Interactive Mode
- Using Command-line
- Using an Integrated Development Environment (IDE)
Let's explore them in detail:
Using Interactive mode:
Python's interactive mode allows us to run code line-by-line directly in the interpreter.
Use these commands to enter interactive mode:
$ python # Unix/Linux
C:\> python # Windows
Example:
Here we will enter the interactive mode and ask Python to solve a simple computation. Look at the image below:

Using command-line:
In this method, we need to create a python file first (ignore if file is already available) and then call interpreter to run it.
Example:
Let's make a python file that simply computes the sum of 5 and 10 and returns the result and save it as gfg.py file. This would look somewhat like the below:

Now execute the file by using the below command:
python gfg.py
This will result in the following:

Using an IDE
IDEs like VSCode, PyCharm, Sublime Text or Jupyter Notebook provide a rich environment for writing, testing and debugging Python code.
Example using Jupyter Notebook:
Here we will write a simple python code and ask the IDE to execute it in Python.

Now if wehit the Run Button on the IDE it will call for the interpreter automatically and execute the program. Below is the output:
Similar Reads
Access environment variable values in Python
An environment variable is a variable that is created by the Operating System. Environment variables are created in the form of Key-Value pairs. To Access environment variables in Python's we can use the OS module which provides a property called environ that contains environment variables in key-va
3 min read
Create virtual environment in Python
A virtual environment is a self-contained directory that contains a Python installation for a particular version of Python, plus a number of additional packages. Using virtual environments is a common practice in Python development as it helps to manage dependencies for different projects, avoiding
3 min read
Read Environment Variables with Python dotenv
Environment variables play a crucial role in the configuration and operation of software applications. They provide a mechanism to store configuration settings that can be used by applications to function properly. This separation of configuration from code allows for more secure and flexible softwa
4 min read
Read Environment Variables with Python dotenv
Environment variables play a crucial role in the configuration and operation of software applications. They provide a mechanism to store configuration settings that can be used by applications to function properly. This separation of configuration from code allows for more secure and flexible softwa
4 min read
Viewing all defined variables in Python
In this article, we are going to discuss how to view all defined variables in Python. Viewing all defined variables plays a major role while debugging the code. Method 1: Using dir() function dir() is a built-in function to store all the variables inside a program along with the built-in variable fu
5 min read
Python Variables
In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Managing Virtual environments in Python Poetry
Poetry helps you declare, manage, and install dependencies of Python projects, ensuring you have the right stack everywhere. Poetry is a tool for dependency management and packaging in the PHP programming language that helps in managing project dependencies and creating virtual environments. Unlike
4 min read
How to use Variables in Python3?
Variable is a name for a location in memory. It can be used to hold a value and reference that stored value within a computer program. the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to the variables, you can store
3 min read
How To Print A Variable's Name In Python
In Python, printing a variable's name can be a useful debugging technique or a way to enhance code readability. While the language itself does not provide a built-in method to directly obtain a variable's name, there are several creative ways to achieve this. In this article, we'll explore five simp
3 min read
Print Single and Multiple variable in Python
In Python, printing single and multiple variables refers to displaying the values stored in one or more variables using the print() function.Let's look at ways how we can print variables in Python:Printing a Single Variable in PythonThe simplest form of output is displaying the value of a single var
2 min read