【问题标题】:Execute python script from everywhere从任何地方执行 python 脚本
【发布时间】:2013-02-02 07:52:49
【问题描述】:

我想在路径中添加一些 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 linux bash


    【解决方案1】:

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

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

    【讨论】:

    • 我在一个虚拟环境中。我得打电话给./test.2.py
    【解决方案2】:

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

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

    #!/usr/bin/python
    

    然后输入:

    test.2.py
    

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 1970-01-01
      • 2013-03-06
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      相关资源
      最近更新 更多