从任何地方执行python脚本 [英] Execute python script from everywhere

查看:69
本文介绍了从任何地方执行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to add some python script to path.

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

I can add bash scripts to folders in my path and then execute them from everywhere. When I do so with python script, I can only execute them when I am in the same directory.

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

Per exemple, if I put test and test2.py in the same folder in my path.

这项工作:

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源文件位于路径上的目录中,请执行以下操作:

Assuming the python source file is in a directory that is on your path do the following:

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

这篇关于从任何地方执行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆