简体   繁体   中英

How to make a python program run just by typing the name alone at the terminal?

I am not sure what I am supposed to be searching, because everything I have been trying to look up has not really given me the answers that I need.

If anyone can point me in the right direction of exactly what I should be looking at, I would greatly appreciate it. Basically, I want to know the best way to go about making a python program which I can distribute (not publicly).

Once someone has the file they run a script, or run the make command, and the result of this would be that the user can invoke the program from within any directory on the computer by just typing the name (and passing args) in the terminal.

I only know of one way to do this, which I believe is the wrong way. My idea was something as follows:

1.) A main folder, let's call it Ghoul. It would contain 3 files:

 setup.sh ghoul.sh ghoul.py 

2.) The user would have to run setup.sh, which would make ghoul.sh an executable and it would change the name to ghoul and then I would move it to /bin

3.) Within the ghoul file which is now in /bin, the file itself would contain "python /home/user/Ghoul/ghoul.py $@" And this would allow the program to run from any directory just by typing "ghoul"

I hope it's clear what I am trying to accomplish, I know this can't be the appropriate way of doing this. What should I be looking up and reading on to accomplish this? I would like to know how I can do this on Ubuntu and if also ..Windows.

I am also interested if I can limit the "ghoul" command to work only in the Ghoul directory, but of course in any directory that is within the Ghoul directory instead of from within any directory on the system.

Thank you.

On Unix like systems

#! /usr/bin/env python

in the very first line of your chmod +x script file will make it executable from the current directory using ./filename

If you want completely straightforward execution from anywhere you can put it on the path somewhere however you choose.

On windows its more complicated, if you want commandline execution the default install should give you the ability to execute

myscript.py

If its in your path. to drop the .py you need to get .py into the PATHEXT variable. Alternatively if it neds to be distributed to someone without any python install check out www.py2exe.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM