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
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 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
Python | os.environ object os.environ in Python is a mapping object that represents the userâs OS environmental variables. It returns a dictionary having the 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
5 min read
Creating Python Virtual Environment in Windows and Linux A Virtual Environment is a Python environment, that is an isolated working copy of Python that allows you to work on a specific project without affecting other projects So basically it is a tool that enables multiple side-by-side installations of Python, one for each project. Creating a Python virtu
1 min read
__name__ (A Special variable) in Python Since there is no main() function in Python, when the command to run a python program is given to the interpreter, the code that is at level 0 indentation is to be executed. However, before doing that, it will define a few special variables. __name__ is one such special variable. If the source file
2 min read
Python | Set 2 (Variables, Expressions, Conditions and Functions) Introduction to Python has been dealt with in this article. Now, let us begin with learning python. Running your First Code in Python Python programs are not compiled, rather they are interpreted. Now, let us move to writing python code and running it. Please make sure that python is installed on th
3 min read