Environment Variables in Python
Last Updated :
01 Oct, 2020
In Python, its behavior is highly influenced by the setup of the environment variables. There is a fixed number of environment variables that Python recognizes and these generally are processed before the command line switches. Whenever a conflict arises between the environmental variable and the command line switches, the environment variable gets overridden.
Different Environment variable
In this article, we will explore some of the most prominent environment variables in Python.
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 Modules. The PYTHONPATH variable holds a string with the name of various directories that needs to be added to the sys.path directory list by Python. The primary use of this variable is to allow users to import modules that are not made installable yet.
PYTHONHOME
This variable is used to set the default location of the standard Python libraries. By Default, Python searches for its libraries in prefix/lib/pythonversion and exec_prefix/lib/pythonversion. Here the prefix and exec_prefix are installation-dependent directories, and both of them default to /usr/local.
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
If the PYTHONINSPECT variable is an empty string, it compels python to enter into an interactive python-mode and ignore the PYTHONSTARTUP files. 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.
PYTHONCASEOK
This environment variable is used to ignore all import statements while calling the Python interpreter. In a Windows machine, it is used to find the first case-insensitive match in an import statement.
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.
The above variables are the primary environment variables that are frequently used.
Ways of executing code in Python
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:
In this mode, you basically call the Python interpreter and throw a bunch of commands to it to execute. To enter into the interactive mode of Python use the below commands:
$python # Unix/Linux
or
python% # Unix/Linux
or
C:> python # Windows/DOS
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 of using python, you need to call the python interpreter first and then ask it to run a python file.
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
There are plenty of IDEs available on the internet like VScode, Sublime editor, and pycharm, etc. Here we will demonstrate the use of python using Jupyter Notebook, a python IDE from Anaconda.
Example:
Here we will write a simple python code and ask the IDE to execute it in Python.

Now if you hit the Run Button on the IDE it will call for the interpreter automatically and execute the program. This would look like below:

Similar Reads
Environment Variables in Python
In Python, its behavior is highly influenced by the setup of the environment variables. There is a fixed number of environment variables that Python recognizes and these generally are processed before the command line switches. Whenever a conflict arises between the environmental variable and the co
4 min read
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
6 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 this article, we are going to see how to assign a function to a variable in Python. In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Implementation Simply assign a function to the de
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. This is a basic way to see the output of our code or debug our program. Printing a Single Variable in PythonIn Python, the print() function is used to display
1 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. This is a basic way to see the output of our code or debug our program. Printing a Single Variable in PythonIn Python, the print() function is used to display
1 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
EnvironmentError Exception in Python
EnvironmentError is the base class for errors that come from outside of Python (the operating system, file system, etc.). It is the parent class for IOError and OSError exceptions. exception IOError - It is raised when an I/O operation (when a method of a file object ) fails. e.g "File not found" or
1 min read
How to import variables from another file in Python?
When the lines of code increase, it is cumbersome to search for the required block of code. It is a good practice to differentiate the lines of code according to their working. It can be done by having separate files for different working codes. As we know, various libraries in Python provide variou
2 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
2 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
Variable Shadowing in Python
In this article, we will understand the concept of variable shadowing in a Python programming language. To understand this concept, we need to be well versed with the scope of a lifetime of variables in python. Local variables:When we define a function, we can create variables that are scoped only t
3 min read
Relative Import in Python
In Python, relative imports allow us to import modules in relation to the current module's location within a package structure. This means instead of using full paths, we can import modules using . (current directory) or .. (parent directory), making the code more concise and easier to maintain. Rel
2 min read
Relative Import in Python
In Python, relative imports allow us to import modules in relation to the current module's location within a package structure. This means instead of using full paths, we can import modules using . (current directory) or .. (parent directory), making the code more concise and easier to maintain. Rel
2 min read