I have a small program in python that consists in one .py file plus a directory of data files used by the program.
I would like to know the proper way to create an installation procedure for a user with admin rights on Linux so that he can install the program on his system and use it from the command line, with options and parameters. EDIT: The part I am having trouble with is to have the program, once installed, retrieve the data files, contained in a 'data' sub-folder.
Would a setup script that installs the executable program file in /usr/local/bin and the data folder in /usr/share/my_program/data be an acceptable solution? Something like:
#!/bin/bash
# Launch with sudo
chmod +x program.py
cp program.py /usr/local/bin
cp -r data /usr/share/my_program
echo Installation complete
Now, in order to do that, I have to assume, in the program, that the data files are going to be in /usr/share/my_program/data. But I would also leave the user the choice of using the program without installing it. Then I would have to assume that the data are in './data', relative to the executable program file. How should I solve this problem? I can think of a few ways, but my feeling is that I am creating a mess where there should be a clear and correct answer.
Currently, I am considering using a try except clause:
try:
find data from /usr/share/my_program && set path accordingly
except:
set path to the data as './data'
Again, I feel it is a bit convoluted. How would you proceed for the installation?
Many thanks
EDIT: SOLUTION ADOPTED
Based on the answers of this question, and to those in the question suggested by FakeRainBrigand (How to know the path of the running script in Python?), I created an install script that goes like this:
#!/bin/bash
mkdir /usr/share/my_program
chmod +x my_program.py
cp my_program.py /usr/local/bin
cp -r data /usr/share/my_program
echo Installation completed
And added the following code in my program:
if os.path.dirname(__file__) == "/usr/local/bin":
DATA_PATH = "/usr/share/my_program/data"
elif os.path.dirname(__file__) == ".":
DATA_PATH = "./data"
else:
print "You do not have a working installation of my_program"
print "See the installation procedure in the README file"
sys.exit(1)
I then use os.path.join(DATA_PATH, "file-to-reach.txt") so that the program can reach it's data, found under /usr/share/my_program.
I would be glad to have comments if a more accepted method is available.
Cheers