Making Python script accessible system wide

后端 未结 2 686
一生所求
一生所求 2020-12-18 11:37

Can someone tell me how to make my script callable in any directory?

My script simply returns the number of files in a directory. I would like it to work in any dire

相关标签:
2条回答
  • 2020-12-18 12:09

    If your script starts with a suitable shebang line, such as:

    #!/usr/bin/env python
    

    And your script has the executable bit set (for Linux, OS X, and other Unix-like systems):

    chmod +x myscript.py
    

    And the path to your script is in your PATH environment variable:

    export PATH=${PATH}:`pwd` # on Unix-like systems
    
    SET PATH=%PATH%;\path\to # on Windows
    

    Then you can call myscript.py from wherever you are.

    0 讨论(0)
  • 2020-12-18 12:14

    All of those operating systems should support a PATH environment variable which specifies directories that have executables that should be available everywhere. Make your script executable by chmod +x and place it into one of those directories (or add a new one to your PATH - I have ~/bin for instance).

    I don't know how to make new kinds of files directly executable on Windows, though, but I guess you could use a .bat file as a proxy.

    0 讨论(0)
提交回复
热议问题