PYTHONPATH Environment Variable in Python
Last Updated :
05 Sep, 2020
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 Modules. The PYTHONPATH variable holds a string with the name of various directories that need 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. Let's try to understand the concept using an example.
Suppose you defined a module as below:
Python3
# user-defined module
# saved as my_module.py
def module_func():
print("You just imported the user-defined module")
Now if you attempt to import the_my_module.py in your python script as below:
Python3
# python script to
# call the module
# import the module
import my_module
# call the module function
my_module.module_func()
This will lead to the following error:
This is due to the reason that PYTHONPATH is not set yet. In simpler words, the Python interpreter cannot find the location of my_module.py file. So to set PYTHONPATH on a windows machine follow the below steps:
Step 1: Open your This PC (or My Computer) and write click and click on properties.
Step 2: After the properties window pop up click on to Advance System Settings:
Step 3: Now click on the environment variable button in the new popped up window as shown below:
Step 4: Now in the new Environment Variable dialog box click on New as shown below:
Step 5: Now in the variable dialog box add the name of the variable as PYTHONPATH and in value add the location to the module directory that you want python to check every time as shown below:
Step 6: Now open your command prompt and execute the my_script.py file with the below command:
python my_script.py
The behavior of python is set now so it should have no problem executing the python script by importing the my_module.py as shown below:
Similar Reads
Environment Variables in Python 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 co
3 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
Read Environment Variables with Python dotenv Environment variables play a crucial role in the configuration and operation of software applications. They provide a mechanism to store configuration settings that can be used by applications to function properly. This separation of configuration from code allows for more secure and flexible softwa
4 min read
Convert String into Variable Name in Python There may be situations where you want to convert a string into a variable name dynamically. In this article, we'll explore how to convert a string into a variable name in Python with four simple examples. Convert String into Variable Name in PythonWhile Python does not directly allow you to convert
3 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 Setup Anaconda Path to Environment Variable? Anaconda is open-source software that contains Jupyter, spyder, etc that are used for large data processing, data analytics, and heavy scientific computing. Anaconda works for R and Python programming languages. Spyder(a sub-application of Anaconda) is used for Python. for Python will work in Spyder
4 min read