Home > python > Import a file from another directory of the project

Import a file from another directory of the project

Imagine the project structure on the right side. The project is called “Sortings”, thus everything is under this directory. It’s a good idea to put your project in a package, thus all the source files are in the “sortings” package. Here we have a “config.py” file that is used by almost all the source files in the project. We also have a subpackage called “sorts”, and in this subpackage there is a script called “shell.py”. Say “shell.py” wants to include “config.py” from the parent directory. It can be done this way:

# shell.py
from sortings import config

Now you see why we put everything under the named package “sortings”.

It works well from the IDE, great. However, if you go to the command-line and you want to launch “shell.py”, you’ll get a surprise:

$ ./shell.py
Traceback (most recent call last):
  File "./shell.py", line 3, in
    from sortings import config
ImportError: No module named sortings

Hmmm… Let’s see the PYTHONPATH:

$ echo $PYTHONPATH
:/home/jabba/python/lib/jabbapylib:/home/jabba/python/lib

He is right, the “Sortings” directory is not in the PYTHONPATH, thus it cannot find the module “sortings”.

Let’s add the required directory then. Open “shell.py” and add these lines to the beginning. It is important that these lines should be executed first.

import sys
sys.path.append('../..')

from sortings import config
...

Now it works… as long as you launch “shell.py” from the current directory. Step up to the parent directory and try to launch it again:

$ ./shell.py    # works
$ cd ..
$ sorts/shell.py
Traceback (most recent call last):
  File "sorts/shell.py", line 6, in
    from sortings import config
ImportError: No module named sortings

Damn, what’s wrong again? Well, we appended a relative path, not an absolute path. Let’s change the beginning of “shell.py” again:

import os
import sys
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '../..'))

from sortings import config
...

Super! Now you can call “shell.py” from everywhere, even from crontab.

Another way
There is another way too. Add the absolute path of the directory “Sortings” to PYTHONPATH. If you want it permanent, add the following to your “.bashrc” file:

export PYTHONPATH=$PYTHONPATH:/abs/path/to/Sortings

Note that this is not the recommended way though.

About these ads
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 79 other followers

%d bloggers like this: