[英]How do I make my python file executable from anywhere in my system and without having to say "python3" and ".py" before and after the file name
I am trying to make my python3 file executable from any location in the terminal (on mac os) so that instead of:我正在尝试使我的 python3 文件可以从终端中的任何位置(在 mac os 上)执行,而不是:
python3 myFile.py argv1 argv2 argv 3
Instead you type相反,您键入
myFile argv1 argv2 argv3
I can make the file executable via./myFile but that's not really what I'm looking for and I can't make it executable from anywhere in the system.我可以通过 ./myFile 使文件可执行,但这并不是我真正想要的,我不能让它在系统中的任何地方都可以执行。
To make it executable without saying python
first, put要使其可执行而不首先说
python
,请输入
#!/usr/bin/env python
as the first line and make the file executable.作为第一行并使文件可执行。
To make it executable from any location, put the script in a directory that's in your $PATH
environment variable.要使其从任何位置可执行,请将脚本放在
$PATH
环境变量中的目录中。
Type which python3
to uncover where you have your python executable.键入
which python3
以发现 python 可执行文件的位置。 In my case it is /usr/local/bin/python3
directory.在我的例子中,它是
/usr/local/bin/python3
目录。
Your python file should contain a shebang referencing the executable as the first line in the file.您的 python 文件应该包含一个 shebang 引用可执行文件作为文件的第一行。 So, for example:
因此,例如:
#!/usr/local/bin/python3
Next, you must make sure that the file itself is executable.接下来,您必须确保文件本身是可执行的。
Some files have different permissions (files can be read / write / execute and by different groups user / group / everyone).有些文件有不同的权限(文件可以被不同的组用户/组/每个人读/写/执行)。 Execute on the terminal
sudo chmod +x your_python_file.py
.在终端上执行
sudo chmod +x your_python_file.py
。
Finally, you should add the directory where your file script is saved to your system $PATH.最后,您应该将保存文件脚本的目录添加到系统 $PATH。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.