Open In App
Related Articles

Environment Variables in Python

Like Article
Save Article
Report issue

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. 


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.


Whenever the python interpreted is first initialized Python looks for a readable file with the name 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.


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.


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.


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:

  1. Using Interactive Mode
  2. Using Command-line
  3. 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
python% # Unix/Linux
C:> python # Windows/DOS


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.


Let’s make a python file that simply computes the sum of 5 and 10 and returns the result and save it as file. This would look somewhat like the below:

Now execute the file by using the below command:


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.


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:

Last Updated : 01 Oct, 2020
Like Article
Save Article
Share your thoughts in the comments
Similar Reads