I have a small program in python that consists of one .py file and a directory of data files used by the program.
I would like to know how to correctly create the installation procedure for a user with administrator rights on Linux so that he can install the program on his system and use it from the command line with parameters and parameters. EDIT: I am having problems so that the program, after installation, retrieves the data files contained in the "data" subfolder.
Would installing a script that installs the program executable in /usr/local/bin and the data folder in /usr/share/my_program/data be an acceptable solution? Something like:
#!/bin/bash
Now, to do this, I have to accept in the program that the data files will be in /usr/share/my_program/data . But I would also leave the user the opportunity to use the program without installing it. Then I would have to assume that the data is in './data', relative to the program executable. How do I solve this problem? I can think of several ways, but I feel like I'm creating a mess where there should be a clear and correct answer.
I am currently 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 this is a bit confusing. How do you proceed with the installation?
Many thanks
EDIT: DECISION ADOPTED
Based on the answers to this question and the question suggested by FakeRainBrigand ( How do I know the path to running a script in Python? ), I created a script installation that looks something 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)
Then I use os.path.join(DATA_PATH, "file-to-reach.txt") so that the program can bring to it the data found under /usr/share/my_program .
I would be happy to receive comments if a more acceptable method is available.
Greetings