繁体   English   中英

从任何地方执行python脚本

[英]Execute python script from everywhere

我想在路径中添加一些python脚本。

我可以将bash脚本添加到路径中的文件夹,然后从任何地方执行它们。 当我使用python脚本执行此操作时,只能在同一目录中执行它们。

例如,如果我将test和test2.py放在路径的同一文件夹中。

这项工作:

 sh test success hello world 

这不是:

 python test.2.py python: can't open file 'test2.py': [Errno 2] No such file or directory [Errno 2] No such file or directory 

假设python源文件位于路径上的目录中,请执行以下操作:

  1. 将此行添加到python文件的顶部: #!/usr/bin/env python
  2. 将您的python文件设置为可执行文件: chmod +x test.2.py
  3. 使用以下命令运行python脚本: test.2.py

python命令不会像bash一样在$PATH搜索脚本。

使test.2.py可执行,并使其第一行:

#!/usr/bin/python

然后输入以下内容运行它:

test.2.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM