Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a Python script that looks files up in a relative directory. For example: the Python script is in /home/username/projectname/. I have a file that is being called within the Python script that is in /home/username/projectname/subfolder.

If I run the script from the shell as python scriptname.py it runs perfectly fine.

However, I'm trying to run the script as a startup service. I'm setting it up in webmin, and I believe its creating a shell script to call it. Through the startup script, I'm doing something like this to call the script:

execute python home/username/projectname/scriptname.py

The script is starting up fine, but it can't access the files in the relative directory.

I am guessing that there is a better way to call the Python program from within the startup script so that its aware of the relative path.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Quick and dirty:

In your start up script instead of just executing the python script, use cd first.

#!/bin/sh

cd /home/username/projectname &&
python ./scriptname.py
share|improve this answer
    
well, I might have been mistaken in saying that its a startup script. It looks like I can just put parameters in. There are two fields: "Server program and parameters" and "Commands to run before starting server (Optional)". I've tried variants of the suggested option, but with no luck. If I put the whole thing in the parameter, it wont create. If I put the cd line in the "optional" it wont start. –  Randy Jul 8 '12 at 23:30
add comment

An even faster, dirtier way of doing it (with a subshell):

$ ( cd my/path/to/folder && python myprogram.py )
share|improve this answer
add comment

There are a couple of ways around this directly in your python script...

First, if your script is always going to be in "/home/username/projectname/subfolder", you can simply add that to your search path inside python:

import sys
sys.path.append("/home/username/projectname/subfolder")

I suspect, however, that you might have this in multiple "projectname" directories, so a more generic solution is something like this:

import sys
import os
sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), "subfolder"))

this finds the directory where the python script is (in sys.argv[0]), extracts the directory part, appends "subfolder" onto it, and puts it into the search path.

Note that some operating systems may only give the executable name in sys.argv[0]. I don't have a good solution for this case... perhaps someone else does. You may also need to inject a os.path.abspath() call in there if sys.argv[0] has a relative path, but play around with it a bit and you should be able to get it working.

Just realized that I never mentioned the second solution... similar to the earlier answer, you can have the python script change directories all by itself (no need for a wrapper script):

import os
os.chdir("/home/username/projectname")
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.