The Wayback Machine - https://web.archive.org/web/20240710005045/https://www.geeksforgeeks.org/pythonpath-environment-variable-in-python/
Open In App

PYTHONPATH Environment Variable in Python

Last Updated : 05 Sep, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Image

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:

Image

Step 3: Now click on the environment variable button in the new popped up window as shown below:

Image

Step 4: Now in the new Environment Variable dialog box click on New as shown below:

Image

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:

Image

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:

Image



Similar Reads

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-value pairs. In this article, we will see how to use
3 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. Package versions are managed by the package mana
4 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 virtual environment in Linux If pip is not in your syst
2 min read
Add packages to Anaconda environment in Python
Let's see some methods that can be used to install packages in the Anaconda environment. There are many ways one can add pre-built packages to an anaconda environment. So, let's see how to direct the path in the anaconda and install them. Add packages to the Anaconda environment using the navigator Step 1: Open your Anaconda navigator Step 2: Go to
2 min read
Using mkvirtualenv to create new Virtual Environment - Python
Virtual Environment are used If you already have a python version installed and you want to use a different version for a project without bothering the older ones. it is good practice to use a new virtual environment for different projects. There are multiple ways of creating that, today we will create one using mkvirtualenv command. virtualenvwrap
2 min read
Python - Setting up the Bokeh Environment
Bokeh is supported on CPython versions 3.6+ only both with Standard distribution and Anaconda distribution. Other Python versions or implementations may or may not function. Current version of Bokeh is 2.0.2 . Bokeh package has the following dependencies: 1. Required Dependencies PyYAML>=3.10python-dateutil>=2.1Jinja2>=2.7numpy>=1.11.3p
1 min read
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 command line switches, the environment variable gets
4 min read
Python Falcon - Environment Setup
Falcon is a Python library renowned for its proficiency in crafting indispensable REST APIs and microservices. Capable of accommodating both the WSGI (Web Server Gateway Interface) and ASGI (Asynchronous Server Gateway Interface) standards, Falcon has gained widespread recognition since its inception. Originally conceived by Kurt Griffiths in Janua
3 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. Environment Setup for Python PyramidTo begin wit
3 min read
Create virtual environment using venv | Python
Managing multiple Python projects that have conflicting dependencies can be a daunting task. However, virtual environments can help you solve this problem. With virtual environments, each project can thrive in its own sandboxed paradise with its libraries and settings. By learning how to create virtual environments using Venv, you can say goodbye t
6 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg