Posts Tagged ‘PYTHONPATH’

Import a file from another directory of the project

March 31, 2012 1 comment

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 “” 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 “”. Say “” wants to include “” from the parent directory. It can be done this way:

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 “”, you’ll get a surprise:

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

Hmmm… Let’s see the PYTHONPATH:


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 “” and add these lines to the beginning. It is important that these lines should be executed first.

import sys

from sortings import config

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

$ ./    # works
$ cd ..
$ sorts/
Traceback (most recent call last):
  File "sorts/", 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 “” again:

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

from sortings import config

Super! Now you can call “” 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.

Calling a Python script from crontab

March 31, 2012 6 comments

You want to call a Python script of yours from crontab but it doesn’t really want to work :(

First, redirect everything to a file, thus you can check the error messages. For testing, let’s call the script at every minute. Edit your crontab settings with “crontab -e“.

* * * * * python /absolute/path/to/ >>/tmp/out.txt 2>&1

Don’t redirect to /var/log/cronrun (as many guides suggest), because if you have no rights to modify it, you won’t see anything… Choose a file where you can write to.

Then start checking the output of the log file:

touch /tmp/out.txt
tail -f /tmp/out.txt

tail -f” monitors the file constantly and prints every change.

Bare in mind that crontab doesn’t read your startup settings from .bashrc for instance, thus if you added something to PYTHONPATH, it won’t be visible! You have to repeat your PYTHONPATH settings in crontab too. “Each directory you need will have to be listed, because you can’t append to it since the previous value isn’t available.” (tip from here)

* * * * * python /absolute/path/to/ >>/tmp/out.txt 2>&1

If it works, customize the execution time of the script by changing the “* * * * *” part.

You may also have problems with imports. Say your script wants to import another module from a different folder. In this case you can have a look at this post: Import a file from another directory of the project.

Related posts

Categories: python Tags: ,

Understanding imports and PYTHONPATH

April 23, 2011 Leave a comment

If you have problems with imports or you want to know how to write your own library and make it globally available, read Dan Fairs’ excellent article entitled Understanding imports and PYTHONPATH.

Categories: python Tags: , ,