python 在linux环境执行

执行python脚本

脚本hello.py 内容
print 'Hello world!'
  1. 相对路径
    • cd 到脚本所在的路径
    • 执行脚本python hello.py
ute@debian:~/workspace/test$ python hello.py
Hello, world!
  1. 绝对路径
    • 执行python /scriptpath/hello.py
ute@debian:~$ python workspace/test/hello.py
Hello, world!
  1. 其他执行方式
    • 脚本内容变为
#!/usr/bin/env python
print 'Hello world!'
* 给脚本加上权限 `chmod 777 hello.py`
* 在脚本目录下可以执行命令 `./hello.py`
ute@debian:~/workspace/test$ ./hello.py
Hello, world!
* 给脚本换个名字, 执行命令
ute@debian:~/workspace/test$ ls
hello.py
ute@debian:~/workspace/test$ cp hello.py hello
ute@debian:~/workspace/test$ ls
hello  hello.py
ute@debian:~/workspace/test$ hello
Hello, world!

在任何目录执行python脚本--基于上述(3. 其他执行方式)为前提

  1. 只在当前shell界面有效
    • 设置环境变量
export PATH=$PATH:/home/ute/workspace/test/
* 执行命令
ute@debian:~$ hello.py
Hello world!
ute@debian:~$ hello
Hello world!
  1. 在.bashrc里面设置环境
    • 把下面的命令加到.bashrc里面,然后source .bashrc生效
export PATH=$PATH:/home/ute/workspace/test/
* 执行命令
ute@debian:~$ hello.py
Hello world!
ute@debian:~$ hello
Hello world!

别名

  • 把下面的命令加到.bashrc里面,然后source .bashrc生效
alias HELLO="python /home/ute/workspace/test/hello.py"
  • 执行命令
ute@debian:~$ HELLO
Hello world!
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容