简体   繁体   English

可以从OSX Finder和Windows资源管理器运行的Python脚本

[英]Python script that can run from OSX Finder and Windows Explorer

I have a bunch of useful Python3 scripts that I use in different OS environments. 我有很多有用的Python3脚本,可在不同的OS环境中使用。 They run fine when called from the terminal but I want to run them from both Windows Explorer and OSX Finder. 从终端调用它们时,它们运行良好,但我想同时从Windows资源管理器和OSX Finder中运行它们。 In Windows, that is not an issue as the *.py files get associated with the installed interpreter. 在Windows中,这不是问题,因为* .py文件与已安装的解释器相关联。

For OSX, I am having some problems. 对于OSX,我遇到了一些问题。 I tried using the shebang line. 我尝试使用shebang线。

#!/usr/bin/python3

But Finder doesn't recognize it as a script. 但是Finder无法将其识别为脚本。 How do I make Finder recognize *.py files as scripts? 如何使Finder将* .py文件识别为脚本? I am interested in maximum portability with as few OS modifications as possible. 我对最大的可移植性以及尽可能少的OS修改感兴趣。

Also I explicitly specified the interpreter location above. 我也明确指定了上面的解释器位置。 It will be in different locations for different OSes. 对于不同的操作系统,它将位于不同的位置。 Is there a way to make it multi-OS so that if it doesn't find it in one location, it will try another? 有没有一种使其成为多操作系统的方法,以便如果它不在一个位置找到它,它将尝试另一个? Say if I want to run it from one of Ubuntu's GUI File Manager. 假设我想从Ubuntu的GUI文件管理器之一运行它。

EDIT: Here's more info on my use case. 编辑:这是我用例的更多信息。 I want to go to a new computer and git clone my repo of Python3 scripts and be able to run them from within the GUI file manager with minimal changes to the new computer. 我想去一台新计算机,并git复制我的Python3脚本回购,并能够在GUI文件管理器中运行它们,而对新计算机的更改最少。

To make the script more portable use #!/usr/bin/env python3 as shebang. 为了使脚本更具可移植性,请使用#!/usr/bin/env python3作为shebang。 Side effect will be that it will run the first python3 interpreter it finds in the $PATH ( Unix StackExchange question on that subject ) 副作用是它将运行在$ PATH中找到的第一个python3解释器( 关于该主题的Unix StackExchange问​​题


To run the script from the finder try to chmod the file. 要从finder中运行脚本,请尝试chmod文件。 ie chmod +x FILE chmod +x FILE


Found another possibility here: Link to the Blog 在这里找到另一种可能性: 链接到博客

  1. Make this the first line of your Python script "#!/usr/bin/env python" 将其作为Python脚本“#!/ usr / bin / env python”的第一行
  2. Change the extension of the script file to ".command" ie my_python_script.command 将脚本文件的扩展名更改为“ .command”,即my_python_script.command
  3. In Terminal make the Python script file executable by running "chmod +x my_python_script.command" 在终端中,通过运行“ chmod + x my_python_script.command”使Python脚本文件可执行。
  4. Now when you double click the Python script in Finder it will open a terminal window and run. 现在,当您在Finder中双击Python脚本时,它将打开一个终端窗口并运行。

Alternative I found would be this but it needs system modification: Superuser question 我发现的替代方法是这样,但需要系统修改: 超级用户问题

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

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