How to execute python file in linux

前端 未结 7 552
长情又很酷
长情又很酷 2020-12-08 14:27

I am using linux mint, and to run a python file I have to type in the terminal: python [file path], so is there way to make the file executable, and make it run

7条回答
  •  囚心锁ツ
    2020-12-08 15:05

    I suggest that you add

    #!/usr/bin/env python
    

    instead of #!/usr/bin/python at the top of the file. The reason for this is that the python installation may be in different folders in different distros or different computers. By using env you make sure that the system finds python and delegates the script's execution to it.

    As said before to make the script executable, something like:

    chmod u+x name_of_script.py
    

    should do.

提交回复
热议问题