简体   繁体   English

如何使我的 python 文件可以从系统中的任何位置执行,而不必在文件名之前和之后说“python3”和“.py”

[英]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.

相关问题 如何将我的 Python 脚本制作成适用于 Mac 的 Executable .dmg 文件? - How do I make my Python Script into an Executable .dmg file for Mac? 如何使 python 3.7 成为我的默认 python3? - How do I make python 3.7 my default python3? 如何修改和运行 my.py 文件中的一大段代码,而不必每次都读取整个.xlsx 文件? - How do I modify and run a chunk of code from my .py file without having to read the whole .xlsx file every time? 如何从我的 python 类创建一个可调用的函数,而不必在调用时运行整个文件? - How to make a call-able function from my python Class, without having to run the entire file if called? 如何将具有多个 python 文件的 python 项目和 main.py 的输入视频转换为单个可执行文件? - How do i convert python project having many python files and input video for the main.py as a single executable file? PYTHON:如何使程序生成具有随机文件名的文件 - PYTHON : How do I make my program produce a file with a random file name 如何让我的 python function 从 another.py 文件中读取数据 - How do I get my python function to read data from another .py file 在我使它可执行之前,我需要隐藏我的 python 代码。 一旦我使文件可执行文件,内容不应显示给任何人 - I need to hide my python code before i make it executable. Once I make the file executable file the contents should not be shown to anyone 为什么我的所有 xtick 标签前总是有相同的数字? (Matplotlib,Python3) - Why do I keep having this same number before all my xtick labels? (Matplotlib, Python3) python - 我可以将我的文件命名为logging.py - python - can i name my file as logging.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM