Environment Variables in Python - GeeksforGeeks

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.
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 .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.
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.
There are 3 standard ways of using Python, namely:
Using Interactive ModeUsing Command-lineUsing an Integrated Development Environment (IDE)
Using Interactive Mode
Using Command-line
Using an Integrated Development Environment (IDE)
Let’s explore them in detail.
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:
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:
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:
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Reading and Writing to text files in Python
sum() function in Python