Turn an application or script into a shell command

后端 未结 6 1851
走了就别回头了
走了就别回头了 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:19

    Add the directory that the script is in to your path, make it executable, and add a proper shebang line.

    In your .bashrc:

    PATH=$PATH:/dir/to/the/script
    

    Executable:

    chmod +x myscript.py
    

    At the top of the script, add the shebang line:

    #!/usr/bin/env python
    

    Then, from anywhere, you can just do:

    myscript.py
    

    (Note that you don't need a .py suffix, it could be called anything, e.g. app if you have a proper shebang line).

提交回复
热议问题