1.在文件首行添加如下代码:

如果你想使用默认python版本执行当前的脚本,即python2,则需要添加

#!/usr/bin/env python
# -*- coding: utf-8 -*-

 

如果你想使用python3执行当前脚本,则需要添加

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

 

2.将python文件的扩展名改为command。如:

mv test.py test.command

 

3.为脚本添加可执行权限

chmod +x test.command

 

4.完成上面3步,双击脚本就可以自动执行了。

 

注意:

如果你的python文件中需要使用文件当前的相对路径,需要先切换到相对路径后,才可以继续使用相对路径。

解决的办法是:

1.导入系统库

import sys

2.使用python代码切换到当前目录下

#切换到当前目录 python3的写法,python2的写法请自行搜索
os.chdir(sys.path[0])

 

参考链接:

https://blog.csdn.net/Leo____Wang/article/details/71663591

https://blog.csdn.net/lovelyaiq/article/details/78417221

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
  • 2022-12-23
  • 2021-07-10
  • 2021-08-04
  • 2021-07-07
猜你喜欢
  • 2021-06-03
  • 2021-11-18
  • 2022-12-23
  • 2021-09-17
  • 2021-07-28
  • 2021-06-30
相关资源
相似解决方案