Environment variables influence the behavior of Python. PYTHONPATH is one such environment variable; that is, it is a key-value pair stored in a computer's memory. It is available to all programs running in the same environment. 

Master Web Scraping, Django & More!

Python Certification CourseENROLL NOW
Master Web Scraping, Django & More!

What Is the PYTHONPATH Environment Variable in Python?

Pythonpath is a special environment variable that provides guidance to the Python interpreter about where to find various libraries and applications. It is similar to the PATH environment variable in other languages, such as C and Java, but has additional directories for Python modules.

Pythonpath environment variable in Python allows you to include paths to other Python files in your scripts and Python's import mechanism to determine where to search for modules and files that use it. This can be useful if you want to access different pieces of functionality from within your script without having to type out the full path every time.

Pythonpath can be either a single directory or a list of directories separated by colons (:). 

  • If Pythonpath is set to a single directory, Python will search for modules and files in that directory only. 
  • Or if Pythonpath is set to a list of directories, Python will search for modules and files in each of those directories.

When you launch the Python interpreter, it looks for Pythonpath inside your current working directory. If it doesn't find an entry there, it will search for Pythonpath in each parent folder until it finds one or creates one, if necessary. And it is typically set by the Python installation program, but can be overridden by the user.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Understanding the Pythonpath Environment Variable 

An environment variable that lets you add additional directories where Python looks for packages and modules. As these variables are not needed for Python to run, they are not set for most installations. Python can find its standard library. So, the only reason to use PYTHONPATH variables is to maintain directories of custom Python libraries that are not installed in the site packages directory (the global default location). In simple terms, it is used by user-defined modules to set the path so that they can be directly imported into a Python program. It also handles the default search path for modules in Python. PYTHONPATH variable includes various directories as a string to be added to the sys.path directory list. So, with PYTHONPATH, users can import modules that have not been made installable yet. 

Here is an example. The module sample.py has the following content:

def function():

    print("Function is running")

The script sample_script.py has the following content:

import sample

.function()

Here is the output:

$ python3 scripts/sample_script.py

Traceback (most recent call last):

  File "scripts/sample_script.py", line 1, in <module>

    import sample

ModuleNotFoundError: No module named 'sample'

We get such an output because the PYTHONPATH has not been set yet. In layman's terms, the Python interpreter cannot find the location of the sample.py file. In the next section, we will examine how you can set the PYTHONPATH environment variable on different operating systems. 

Setting Python Environment Variable PYTHONPATH on Mac

To set the python environment variable PYTHONPATH on Mac, follow the given steps:

Step 1: Open the Terminal.

Step 2: In your text editor, open the ~/.bash_profile file. For example: atom ~/.bash_profile;

Step 3: To this file, add the following line at the bottom: 

export PYTHONPATH="/Users/my_user/code"

Step 4: Save this text editor file.

Step 5: Close the terminal.

Step 6: Restart the terminal. You can now read the new settings. 

Type: echo $PYTHONPATH

It would show something like /Users/my_user/code. That is it. PYTHONPATH is set. 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Setting the Python Environment Variable PYTHONPATH on Linux

To set the python environment variable PYTHONPATH on Linux, follow the given steps:

Step 1: Open up the terminal 

Step 2: In your text editor, open the ~/.bashrc  file. For example: atom ~/.bashrc;

Step 3: In this text editor, add this line at the end:

export PYTHONPATH=/home/my_user/code

Step 4: Save this text editor file.

Step 5: Close the terminal application 

Step 6: Restart the terminal application. You can read in the new settings. Type:

echo $PYTHONPATH

It will show something like /home/my_user/code: 

PythonPath_1.

You are done setting the PYTHONPATH on your Linux system.

Setting the Python Environment Variable PYTHONPATH on Windows

Here is how to set PYTHONPATH on a windows machine: 

Step 1: Open My Computer or This PC and right-click on it. Then click on properties.

Step 2: When the properties window pops up, click on the Advance System Settings.

PythonPath_2 

Step 3: Click on the environment variable button that appears in the new popped-up window. Here is how it looks:

PythonPath_3

Step 4: In the dialog box- new Environment Variable, click on New. 

PythonPath_4

Step 5: In the variable dialog box, add the variable's name as PYTHONPATH. Add the location that you want Python to check every time as a value to the module directory. 

PythonPath_5

Step 6: Open the command prompt, execute the python file using the given command: 

python my_script.py 

PythonPath_6

(Here, my_script is the name of the python file). 

Master Web Scraping, Django & More!

Python Certification CourseENROLL NOW
Master Web Scraping, Django & More!

FAQs

1. How do I add an environment variable in Pythonpath?

Following a few steps will allow you to add an environment variable:

  • By setting the Pythonpath environment variable.
  • By adding manually to the path in the file. 
  • By using the os.environ module method. 
  • Then you can also use the os.environ["key"] = "value" syntax to add a single environment variable.

2. How do I create a Pythonpath variable?

To create a Pythonpath variable, you will need to first open the Command Prompt. 

Once the Command Prompt is open, you will need to type in the following command: setx /M Pythonpath "C:\Python27\Lib". 

And you should know that creating a Pythonpath environment variable in Python will be changing depending on your operating system and whether you want the variable to be permanent.

  • On Windows, you can use the set command to create a Pythonpath variable:
    set Pythonpath=C:\python
  • On Mac or Linux, you can use the export command to create a Pythonpath variable:
    export Pythonpath =/usr/local/lib/python

3. What is Pythonpath used for?

Pythonpath is an environment variable that is used to specify the location of Python libraries. It is typically used by developers to ensure that their code can find the required Python libraries when it is run.

4. Where is Pythonpath located?

Pythonpath is typically located in the same directory as the Python interpreter.

5. Is SYS path the same as Pythonpath?

  • The SYS path is a list of directories that Python interprets to search for when it starts up. 
  • The Pythonpath is a list of directories that the Python interpreter will search for when it tries to resolve a module name. 

So, both look the same but are quite different.

Choose The Right Software Development Program

This table compares various courses offered by Simplilearn, based on several key features and details. The table provides an overview of the courses' duration, skills you will learn, additional benefits, among other important factors, to help learners make an informed decision about which course best suits their needs.

Program Name Automation Testing Masters Program Full Stack Developer - MEAN Stack Caltech Coding Bootcamp
GeoAllAllUS
UniversitySimplilearnSimplilearnCaltech
Course Duration11 Months11 Months6 Months
Coding Experience RequiredBasic KnowledgeBasic KnowledgeBasic Knowledge
Skills You Will LearnJava, AWS, API Testing, TDD, etc.HTML, CSS, Express.js, API Testing, etc.Java, JavaScript, Angular, MongoDB, etc.
Additional BenefitsStructured Guidance
Learn From Experts
Hands-on Training
Blended Learning Program
Learn 20+ Tools and Skills
Industry Aligned Projects
Caltech Campus Connect
Career Services
17 CEU Credits
Cost$$$$$$$$
Explore ProgramExplore ProgramExplore Program

Conclusion

We have reached the end of this blog. We saw how you could set the PYTHONPATH environment variable in Python on all three standard operating systems- Linux, Windows, and macOS. After setting up the PYTHON PATH, you can import user-defined modules. And if you wish to master Python, a certification in Python is all you need! However, if you wish to master full-stack development and build a successful career in this field, our Python Training must be your next step.

And on the other hand, if you have any questions in terms of python path, do write to us in the comment section below, and our experts will get back to you!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 21 Oct, 2024

8 months$ 1,499
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 21 Oct, 2024

6 Months$ 1,449
Full Stack Java Developer Masters Program

Cohort Starts: 6 Nov, 2024

7 months$ 1,449