You are currently viewing Add Package, Directory to PYTHONPATH in Windows?

What is PYTHONPATH in Python and how to add to it in Windows? You may need to use a custom module or package that is not included in the standard library. In these cases, you will need to add the directory containing the package to the PYTHONPATH variable in order for Python to be able to find and import it correctly.

Advertisements

In this article, We will explain, how to add packages, directories, or modules to the PYTHONPATH, whether you have manually created them or downloaded them from a third-party source.

1. What is a PYTHONPATH Variable?

The PYTHONPATH variable is an environment variable that tells Python where to look for modules and packages that are not part of the standard library. When you import a module or package, Python searches for it in the directories listed in the PYTHONPATH variable, as well as in the default system paths.

Adding directories to the PYTHONPATH variable ensures that your Python scripts can find and use the necessary dependencies. If a module or package is not in a directory listed in the PYTHONPATH variable, Python will not be able to find and import it.

You can use the PYTHONPATH variable for managing Python dependencies and is good for working with custom modules or packages that are not included in the standard library.

2. PYTHONPATH vs Adding Python to Path

Adding Python to the PATH is a way to tell the operating system where to find the Python interpreter executable file. This allows you to run Python commands from anywhere on your system.

On the other hand, We use the PYTHONPATH variable to find modules and packages that are not part of the standard library. PYTHONPATH tells Python where to look for these modules and packages.

3. Add Packages to PYTHONPATH in Windows

To add a Directory, file, or Python Package to PYTHONPATH in Windows, follow the below steps:

3.1 Search for “Environment Variables”

In the bottom left corner, there is a search icon for searching settings. Click in there and then Types “Environment Variables”.

Click on “Edit the system environment variables”.

Snapshot: When you Search for Environment Variables in Windows
Snapshot: Step 1

3.2 Navigate to the “Environment Variables” Window

There is a tab for “Environment Variables” Click on it and you will have

Snapshot : Environment Variables Windows
Snapshot: Step 2

3.3 Click “New” Under the User variables

Clicking the “New” button will Open a New Window, Now you have to put the PYTHONPATH and the directory.

Snapshot : When you add the PYTHONPATH variable
Snapshot: Step 3

3.4 Click Okay and You Added Package to PYTHONPATH

Now everything is done. You have added the directory successfully to PYTHONPATH and you are good to you. You can now use this Package, Python code, or whatever you call it in your own project.

4. Add Package to PYTHONPATH with Command

Now, there is an easy way to add a python directory, Package, or module easily to PYTHONPATH with the command line. You can follow the below steps:

  1. Open CMD or Command Line
  2. Run the following command and you are good to go.

# Using command
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

You can verify it by going to the “Environment Variables” Window that we have seen earlier.

5. Summary and Conclusion

We have learned how to add a python package, directory, or module to PYTHONPATH in Windows. This is quite helpful when you are working on complex python projects. We have discussed each step in great detail. I hope this was helpful to you. Let me know if you have any questions.