5

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.

4 Answers 4

10

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
1
  • 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, 2012 at 23:30
8

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

  1. 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.

  2. Similar to the above answer, you can have the Python script change directories all by itself with no need for a wrapper script:

    import os
    os.chdir("/home/username/projectname")
    
7

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

( cd my/path/to/folder && python myprogram.py )
1
  • 1
    It works if as expected without actually going to that directory, but $ symbol should be removed.
    – Konard
    May 6, 2023 at 9:16
1
set PYTHONPATH=PROJECTPATH && python script.py

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .