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
PYTHONPATH Environment Variable in Python
Python's behavior is greatly influenced by its environment variables. One of those variables is PYTHONPATH. It is used to set the path for the user-defined modules so that it can be directly imported into a Python program. It is also responsible for handling the default search path for Python Module
2 min read
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
Python Virtual Environment | Introduction
A Python Virtual Environment is an isolated space where you can work on your Python projects, separately from your system-installed Python. You can set up your own libraries and dependencies without affecting the system Python. We will use virtualenv to create a virtual environment in Python. What i
4 min read
Python Pyramid - Environment Setup
Python Pyramid, often simply referred to as Pyramid, is an open-source web framework used for building web applications and web services in Python. Whether you're constructing a website, or a sophisticated web application Pyramid offers the resources and adaptability to complete the task effectively
3 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
7 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
Assign Function to a Variable in Python
In Python, functions are first-class objects, meaning they can be assigned to variables, passed as arguments and returned from other functions. Assigning a function to a variable enables function calls using the variable name, enhancing reusability. Example: [GFGTABS] Python # defining a function de
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
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 v
2 min read
Python | Set 6 (Command Line and Variable Arguments)
Previous Python Articles (Set 1 | Set 2 | Set 3 | Set 4 | Set 5) This article is focused on command line arguments as well as variable arguments (args and kwargs) for the functions in python. Command Line Arguments Till now, we have taken input in python using raw_input() or input() [for integers].
2 min read