1

Excuse the awkward question wording.

I've made a script. I would like for others to download it from github, and run it by typing programName argument1 argument2, similar to any other popular app used through the terminal such as Jupyter or even opening Atom/Sublime/etc. (ex:jupyter notebook, atom .). However, unlike Jupyter or sublime, my script isn't launching another app, it's a small app meant to be used in the shell.

Currently, to use my script, one must type into the command line python programName.py arg1 etc from within the file's directory.

How do I allow others to dl it and use it from anywhere (not having to be within the directory), without having to type out the whole python programName.py part, and only having to type programName arg1?

Jay Jung
  • 1,805
  • 3
  • 23
  • 46

4 Answers4

4

This blog post explains step by step how to create a distribution that you can install and it would turn into an executable.

You can refer to this github repo for a sample application.

The full documentation of setuptools is available here.

In general, you should configure your setup.py in order to use the command in the entry-point option:

setup(
    name = "your_app_name",
    packages = ["package_name"],
    entry_points = {
        "console_scripts": ['cmd_name = package_name.package_name:main']
    },
    ....
)

This solution would work on every OS where you have installed python.

mabe02
  • 2,676
  • 2
  • 20
  • 35
1

In addition to mabe02: Python is a scripting language and usually not compiled, which means you will need an interpreter to run your program.

Programms made in C f.e. can be run on its own because they were compiled into machine code by a compiler.

An interpreter is similar to a compiler as it reads your script and interprets it at runntime. That is why you need to write python before your programm, to tell your computer to interpret your script on runntime, using python. This doesn't mean that there are no possibilities to compile python as can be seen in the other answer and in this link Can a python program be run on a computer without Python? What about C/C++? (py2exe and py2app).

  • Hi max, thanks for making my question more clearer. Actually I wrote this answer since the OP was bringing `jupyter` as example which is implemented in the way I described. – mabe02 Sep 08 '17 at 08:42
1

Your script may need to have an interpreter, "shebang", besides being "reachable" by the $PATH

#!interpreter [optional-arg]

For example, you could have something like

#!/usr/bin/env python

or to force a specific version

#!/usr/local/bin/python2.7

Next, your script needs to be available within the $PATH, check this answer that covers that part: https://unix.stackexchange.com/q/29608/53084

nbari
  • 25,603
  • 10
  • 76
  • 131
1

You can simply add your script to PATH variable in order to launch it from anywhere.

In Linux distros, you can simply do it by using a bash command PATH=$PATH:/path/to/your/script. Make sure you don't have the space around the "=" operator.

Now, the second thing is you don't want your script to be named as pythonProgram.py.You can simply remove the extension .py from PythonProgram.py by adding a single line to the starting of your script.

Open up your script and at the very begining type #!/usr/bin/python.This should be the first line of your code.This line is called shebang and is used to tell the bash which interpreter to be used for compiling the script.

If everything went right, you will be able to run your script as pythonProgram arg1.

Shiva Gupta
  • 402
  • 1
  • 3
  • 13