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!

你可能感兴趣的:(python 在linux环境执行)