Turn an application or script into a shell command

后端 未结 6 1856
走了就别回头了
走了就别回头了 2021-02-06 11:11

When I want to run my python applications from commandline (under ubuntu) I have to be in the directory where is the source code app.py and run the application with command
<

6条回答
  •  醉话见心
    2021-02-06 11:37

    1. Add a shebang: as the top line of the file: #!/usr/bin/python or #!/usr/bin/python3 (you can use the python -B to prevent generation of .pyc files, which is why I don't use /usr/bin/env)

    2. Make it executable: You will need to do chmod +x app.py

    3. (optional) Add directory to path, so can call it anywhere: Add a directory with your executable to your $PATH environment variable. How you do so depends on your shell, but is either export PATH=$PATH:/home/you/some/path/to/myscripts (e.g. Linux distros which use bash) or setenv PATH $PATH:/home/you/some/path/to/myscripts (e.g. tcsh like in Mac OS X). You will want to put this, for example, in your .bashrc or whatever startup script you have, or else you will have to repeat this step every time you log in.

    app.py will need to be in the myscripts (or whatever you name it) folder. You don't even need to call it app.py, but you can just rename it app.

    If you wish to skip step #3, you can still do ./app to run it if you are in the same directory.

提交回复
热议问题