简体   繁体   English

python3不会从mac shell脚本运行

[英]python3 won't run from mac shell script

I am trying to use Automator on macOS 10.12 to launch a Python 3 script. 我试图在macOS 10.12上使用Automator来启动Python 3脚本。 The script works just fine when I run it from the terminal with the command: python3 my_script.py . 当我使用以下命令从终端运行脚本时,脚本运行正常: python3 my_script.py

Automator has a "Run Shell Script" function that uses the /bin/bash shell. Automator有一个“运行Shell脚本”功能,它使用/ bin / bash shell。 The shell will run scripts with the command: python my_script.py , but this only seems to work for scripts written in Python 2.7. shell将使用以下命令运行脚本: python my_script.py ,但这似乎只适用于用Python 2.7编写的脚本。

My script starts with #!/usr/bin/env python3 , which I thought would direct the shell to the correct python interpreter, but that doesn't seem to be the case. 我的脚本以#!/usr/bin/env python3开头,我认为它会将shell指向正确的python解释器,但似乎并非如此。

As a workaround, I can get the script to run if I insert the full path to the python interpreter: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 , but I see this as suboptimal because the commands might not work if/when I update to Python 3.6. 作为一种解决方法,如果我插入python解释器的完整路径,我可以运行脚本: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 ,但我认为这是次优的,因为命令可能不是如果/当我更新到Python 3.6时工作。

Is there a better way to direct the /bin/bash shell to run Python3 scripts? 有没有更好的方法来指导/ bin / bash shell运行Python3脚本?

由于你有shebang行,你可以做./my_script.py ,它应该运行Python 3。

You can install Python 3 via Homebrew with brew install python3 and use #!/usr/local/bin/python3 as your shebang. 您可以使用brew install python3通过Homebrew安装Python 3,并使用#!/usr/local/bin/python3作为您的shebang。

Not a perfect solution but still better than using the full path of the interpreter. 不是一个完美的解决方案,但仍然比使用解释器的完整路径更好。

You could create ~./bash_profile file and export the path to python 3 bin folder, it would look like 您可以创建~./bash_profile文件并将路径导出到python 3 bin文件夹,它看起来像

export PATH=“path-to-python3-bin-folder:$PATH”

Every time that you enter Terminal, type source ~./bash_profile and now you can call python3 as a function in terminal to call your code. 每次进入Terminal时,键入source ~./bash_profile ,现在可以调用python3作为终端中的函数来调用代码。

On the other hand, I would recommend you to download Anaconda, as you can create virtual environments easily with the version of python that you'd like. 另一方面,我建议你下载Anaconda,因为你可以使用你想要的python版本轻松创建虚拟环境。

If python refers to Python 2 then that*s what you should expect. 如果python引用Python 2那么那就是你应该期待的。 Use python3 in the command line, or defer to the script itself to define its interpreter. 在命令行中使用python3 ,或者按照脚本本身来定义其解释器。

In some more detail, make sure the file's first line contains a valid shebang (you seem to have this sorted); 更详细一点,确保文件的第一行包含有效的shebang(你似乎已将其排序); but the shebang doesn't affect what interpreter will be used if you explicitly say python script.py . 但是如果你明确地说python script.py那么shebang不会影响将使用哪个解释器。 Instead, make the file executable, and run it with ./script.py . 相反,使文件可执行,并使用./script.py运行它。

Actually, you can use env on the command line, too. 实际上,您也可以在命令行中使用env env python3 script.py should work at the prompt, too. env python3 script.py应该在提示符下工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM