Correct installation of a script for a small Python program (Not module) under Linux

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 # Launch with sudo chmod +x program.py cp program.py /usr/local/bin cp -r data /usr/share/my_program echo Installation complete 

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

+8
python installation
source share
3 answers

I am mistaken that the setup.py method (i.e.: installing python setup.py) is only for installing modules that will later be used from the interpreter or other scripts?

Yes. You are wrong.

You can install into the Python scripts directory. Keep reading all that setup.py can do. http://docs.python.org/distutils/setupscript.html#installing-scripts

It is actually very, very simple. And this has nothing to do with Python.

Python script can be run from anywhere. Linux provides you with a wide and wide range of options for running the executable file ( /path/to/my_program.py ).

All of these broad and diverse ways have three things in common. Three things that are central to Linux and have nothing to do with Python.

  • The file must be on PATH .

  • The file must have the execute privilege ( chmod +x ... ).

  • The file should start with #!/usr/bin/env python .

How to do it?

For # 1.

  • You can check the PATH and place the file in any of the directories on the PATH. There are many standard options for this. /usr/local/bin , e.g.

  • You can change the user PATH to include a new directory in it. /path/to , for example, so that /path/to/my_program.py can be found.

For # 2. You do the corresponding chmod +x .

For # 3. You write the corresponding line of code.

Read more in the Linux Shell section. All shell programs have the same three functions. bash . csh , python , perl , etc. etc. etc.


extract data files contained in the subfolder "data", referring, for example, to ./data/this_file.dat .

It will be a bad design.

Linux (outside of Python) provides many ways to find useful data files.

  • Install them in a known place with a known path. /usr/local/share comes to mind. This may not be the best place, but it is quite popular.

  • Use environment variables. MYAPP_HOME , for example, can be set to the location of the file. You can use os.path.join( os.environ['MYAPP_HOME'], 'data', 'this_file.dat' ) Very popular.

  • Use the __file__ variable to find out where your script is located. Then use os.path to find the directory and collect the data path using os.path.join( this_directory, 'data', 'this_file.dat' )

+11
source share

If you do not have external dependencies, such as databases or parameters that need to be configured, you need to run a simple setup.sh script.

 DIR=`pwd` NAME=my_app.py echo "#!/usr/bin/env sh python $DIR/my_app.py" > /usr/sbin/$NAME chmod a+x /usr/sbin/$NAME 

It can get complicated depending on your needs.

+2
source share

You can create an executable zip file containing your script and the data files that it uses. Installation just needs to put this file somewhere in $PATH and make it executable: chmod +x my-program .

python: can zip executables include data files?

If you create an appropriate setup.py that tells you how to install your data files and script, then there are many more options for installing the program, for example,

 $ pip install --user http://example.com/your-program.tar.gz 

where pip can be installed through the system wrapper, your-program.tar.gz , which can be created using python setup.py sdist .

0
source share

All Articles