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?
我如何使用我自己的命令行名称(如“myscript”)运行python脚本,而不必使用“python myscript”。在终端py’?
3 个解决方案
#1
18
-
Add a shebang line to the top of the script:
在脚本顶部添加一行shebang:
#!/usr/bin/env python
# !/usr/bin/env python
-
Mark the script as executable:
将脚本标记为可执行文件:
chmod +x myscript.py
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.)将包含它的目录添加到路径变量中。(如果您想让它保持不变,就必须在.bashrc或.bash_profile中使用。)
export PATH=/path/to/script:$PATH
导出路径= /道路/ /脚本:$路径
#2
0
I usually do in the script:
我通常在剧本里这样写:
#!/usr/bin/python
... code ...
And in terminal:
在终端:
$: chmod 755 yourfile.py
$: ./yourfile.py
#3
0
The best way, which is cross-platform, is to create setup.py
, define an entry point in it and install with pip
.
最好的方法,即跨平台,是创建安装。py,定义一个入口点并使用pip进行安装。
Say you have the following contents of myscript.py
:
假设有myscript.py的以下内容:
def run():
print('Hello world')
Then you add setup.py
with the following:
然后你添加设置。py以下:
from setuptools import setup
setup(
name='myscript',
version='0.0.1',
entry_points={
'console_scripts': [
'myscript=myscript:run'
]
}
)
Entry point format is terminal_command_name=python_script_name:main_method_name
入口点格式是terminal_command_name=python_script_name:main_method_name
Finally install with the following command.
最后安装以下命令。
pip install -e /path/to/script/folder
-e
stands for editable, meaning you'll be able to work on the script and invoke the latest version without need to reinstall
-e代表可编辑,这意味着您可以在不需要重新安装的情况下处理脚本并调用最新版本
After that you can run myscript
from any directory.
之后,您可以从任何目录运行myscript。
#1
18
-
Add a shebang line to the top of the script:
在脚本顶部添加一行shebang:
#!/usr/bin/env python
# !/usr/bin/env python
-
Mark the script as executable:
将脚本标记为可执行文件:
chmod +x myscript.py
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.)将包含它的目录添加到路径变量中。(如果您想让它保持不变,就必须在.bashrc或.bash_profile中使用。)
export PATH=/path/to/script:$PATH
导出路径= /道路/ /脚本:$路径
#2
0
I usually do in the script:
我通常在剧本里这样写:
#!/usr/bin/python
... code ...
And in terminal:
在终端:
$: chmod 755 yourfile.py
$: ./yourfile.py
#3
0
The best way, which is cross-platform, is to create setup.py
, define an entry point in it and install with pip
.
最好的方法,即跨平台,是创建安装。py,定义一个入口点并使用pip进行安装。
Say you have the following contents of myscript.py
:
假设有myscript.py的以下内容:
def run():
print('Hello world')
Then you add setup.py
with the following:
然后你添加设置。py以下:
from setuptools import setup
setup(
name='myscript',
version='0.0.1',
entry_points={
'console_scripts': [
'myscript=myscript:run'
]
}
)
Entry point format is terminal_command_name=python_script_name:main_method_name
入口点格式是terminal_command_name=python_script_name:main_method_name
Finally install with the following command.
最后安装以下命令。
pip install -e /path/to/script/folder
-e
stands for editable, meaning you'll be able to work on the script and invoke the latest version without need to reinstall
-e代表可编辑,这意味着您可以在不需要重新安装的情况下处理脚本并调用最新版本
After that you can run myscript
from any directory.
之后,您可以从任何目录运行myscript。