Archive

Posts Tagged ‘PYTHONPATH’

Import a file from another directory of the project

March 31, 2012 Leave a 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 “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.

Calling a Python script from crontab

March 31, 2012 4 comments

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

Solution
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/script.py >>/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)

PYTHONPATH=/dir1:/dir2
* * * * * python /absolute/path/to/script.py >>/tmp/out.txt 2>&1

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

Troubleshooting
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: , ,
Follow

Get every new post delivered to your Inbox.

Join 75 other followers