How to Import from Local Packages and Modules

Brandon Kessler
The Startup
Published in
3 min readSep 3, 2019

Working with local packages can be frustrating when trying to get the relative paths to link correctly.

Using the PYTHONPATH environment variable simplifies this process and makes importing local packages enjoyable.

Setup

It will be easy to understand this process by using a simple example.

Let’s start by creating an empty folder on our desktop called ‘example’. In there, we’ll create two empty directories, one we’ll call ‘scripts’ and the other can be called ‘pkgs’. Within the ‘scripts’ directory, create a file named ‘sample.py’. In the ‘pkgs’ we’ll create another directory called ‘sample_pkg’ and in there we’ll need three files: ‘__init__.py’ — to make it a package -, ‘math_fun.py’, and ‘print_fun.py’.

Now let’s create some sample code to test out the imports. In the ‘math_fun.py’ file, write:

def add(a, b):
return a + b

def sub(a, b):
return a - b

Then, in the ‘print_fun.py’ file, write:

def name(name):
print(f'Hello {name}')

def animal(animal):
print(f'My favorite animal is the {animal}')

Now we’re done with creating our sample package. We have two modules in the package, each containing two methods, one that does simple math and the other that prints out our inputs to the command line.

Next, let’s write the code in our ‘sample.py’ file for importing and using the package.

from sample_pkg import math_fun, print_fun

print(math_fun.add(1, 5))
print(math_fun.sub(11, 2))
print(print_fun.name('Brandon'))
print(print_fun.animal('dog'))

Now our program is complete and let’s try running ‘sample.py’.

$ python example/scripts/sample.py

As expected, we get an error because Python is unable to locate the module that we’re trying to import

This is a common problem that occurs when trying to access packages stored in different directories and locations.

Use PYTHONPATH to set the path to your packages

The most straightforward solution is to use the PYTHONPATH environment variable and set it equal to the path to your packages. This will allow you to create and save all of your packages in one location, and import them directly into your projects.

To do this, we just have to set the environment variable. In windows, search ‘env’ and click ‘Edit the system environment variables’ option. Next, click ‘Environment Variables’:

Under ‘User variables’ click ‘New’. Variable name is equal to ‘PYTHONPATH’ and variable value will be equal to the path to your packages, for example, ‘C:\Users\username\Desktop\example\pkgs’.

If using linux/mac, the easiest is to update the .bashrc file to add the path. For example:

$ nano ~/.bashrc

Then anywhere, write:

export PYTHONPATH='/mnt/c/Users/username/Desktop/example/pkgs'

Again, replacing above with the path to your packages and the username with your username. Then save and quit with ctrl+x, y, enter.

Run Sample.py

Before running ‘sample.py’ again, we have to close and reopen whatever command line we’re using. Then, run the file again:

$ python example/scripts/sample.py

Everything should run as expected this time. From here, you can create and add any additional packages or modules filled with methods, classes, or variables and import them into any file you’re working from in any directory with a simple import statement.

We can also change the path for PYTHONPATH if we chose to move everything to a different location or rename the directory to something else.

This is the easiest, permanent solution to working with local packages.

Feel free to reach out on LinkedIn for any clarification or to discuss.

--

--