How can I run a python script with my own command line name like 'myscript' without having to do 'python myscript.py' in the terminal?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I usually do in the script:
And in terminal:
Add a shebang line to the top of the script:
#!/usr/bin/env python
Mark the script as executable:
chmod +x myscript.py
Add the dir containing it to your
PATH
variable. (If you want it to stick, you'll have to do this in.bashrc
or.bash_profile
in your home dir.)export PATH=/path/to/script:$PATH
The best way, which is cross-platform, is to create
setup.py
, define an entry point in it and install withpip
.Say you have the following contents of
myscript.py
:Then you add
setup.py
with the following:Entry point format is
terminal_command_name=python_script_name:main_method_name
Finally install with the following command.
-e
stands for editable, meaning you'll be able to work on the script and invoke the latest version without need to reinstallAfter that you can run
myscript
from any directory.