如何让 Spyder 直接从 Windows 资源管理器打开 python 脚本(.py 文件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33817046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:02:58  来源:igfitidea点击:

How to get Spyder to open python scripts (.py files) directly from Windows Explorer

pythonwindowsidespyder

提问by AGandom

I have recently installed the Anaconda distribution on Windows 7 (Anaconda 3-2.4.0-Windows-x86_64). Unlike IDLE, I can't right-click and open a py file in the Spyder IDE. I will have to open Spyder first and then navigate to the file or drag and drop it in the editor. Is there any way to open the file in the editor directly from Widows Explorer?

我最近在 Windows 7 (Anaconda 3-2.4.0-Windows-x86_64) 上安装了 Anaconda 发行版。与 IDLE 不同的是,我无法在 Spyder IDE 中右键单击并打开 py 文件。我必须先打开 Spyder,然后导航到该文件或将其拖放到编辑器中。有没有办法直接从 Widows Explorer 在编辑器中打开文件?

回答by Carlos Cordoba

Right now there is no way to open a file in Spyder from the Windows File Explorer when using Anaconda. But we are working to have this functionality in a future version.

现在,在使用 Anaconda 时,无法从 Windows 文件资源管理器中打开 Spyder 中的文件。但我们正在努力在未来版本中拥有此功能。

It will work by adding an entry to the Open withmenu you can see when doing a mouse right-click over a file on the Explorer.

它将通过向打开方式菜单添加一个条目来工作,您可以在资源管理器上的文件上单击鼠标右键时看到。

回答by PetMetz

I have had a similar problem with another piece of software that I use.

我使用的另一个软件也遇到了类似的问题。

My work around for this problem is to set the file association for .py files to C:\Anaconda\Scripts\spider-script.pyvia the Open withdialog. If you now try to open your File.pyby double clicking you'll receive an error like

我解决此问题的方法是C:\Anaconda\Scripts\spider-script.py通过“打开方式”对话框将.py 文件的文件关联设置为。如果你现在尝试File.py通过双击打开你的,你会收到类似的错误

~\file.py is not a valid Win32 application.

~\file.py 不是有效的 Win32 应用程序。

This can be resolved by editing the spyder-script.pyregistry key:

这可以通过编辑spyder-script.py注册表项来解决:

HKEY_USERS\S-1-5-21-3559708500-1520960832-86631148-1002\Software\Classes\Applications\spyder-script.py\shell\open\command

and replacing the default value "C:\Anaconda\Scripts\spyder-script.py" %1with "C:\Anaconda\python.exe" "C:\Anaconda\Scripts\spyder-script.py" %1. Use the search function for this key if the path isn't the same for your machine, and of course use the appropriate path for your python installation. spyder-script.pyshould now execute in a python shell.

和更换的默认值"C:\Anaconda\Scripts\spyder-script.py" %1"C:\Anaconda\python.exe" "C:\Anaconda\Scripts\spyder-script.py" %1。如果路径与您的机器不同,请使用此键的搜索功能,当然,请为您的 python 安装使用适当的路径。spyder-script.py现在应该在 python shell 中执行。

From the docstring of ftype,

从 ftype 的文档字符串中,

...Within an open command string, %0 or %1 are substituted with the file name being launched through the association.

...在打开的命令字符串中,%0 或 %1 替换为通过关联启动的文件名。

回答by Gustav Delius

With the current version of Anaconda (4.1.0) you can simply right-click on a python script in Windows File Explorer and choose "Open with". The first time you do this you need to select "Choose default program" and then browse to spyder.exe in the Script directory in your Anaconda installation. Also make sure that the "Always use the selected program to open this kind of file" is unchecked and then click OK. From now on spyder.exe will always be listed as one of the options when you select "Open with" from the right-click menu in Windows File Explorer.

使用当前版本的 Anaconda (4.1.0),您只需右键单击 Windows 文件资源管理器中的 Python 脚本,然后选择“打开方式”即可。第一次执行此操作时,您需要选择“选择默认程序”,然后浏览到 Anaconda 安装中 Script 目录中的 spyder.exe。还要确保取消选中“始终使用所选程序打开此类文件”,然后单击“确定”。从现在开始,当您从 Windows 文件资源管理器的右键单击菜单中选择“打开方式”时,spyder.exe 将始终列为选项之一。

回答by Martin Sorgel

What is working very well for me in Windows (10), is associating the *.pyfiles with a batch file (let's say "SpyderBATCH.bat") containing this line :

在 Windows (10) 中对我来说效果很好的是将*.py文件与包含以下行的批处理文件(假设“SpyderBATCH.bat”)相关联:

[ANACONDA_FOLDER_PATH]\pythonw.exe" "[ANACONDA_FOLDER_PATH]\cwp.py" "[ANACONDA_FOLDER_PATH]" "[ANACONDA_FOLDER_PATH]/pythonw.exe" "[ANACONDA_FOLDER_PATH]/Scripts/spyder-script.py" %1  

Where [ANACONDA_FOLDER_PATH]has to be replaced with the full path to the Anaconda folder (usually under "Program Files").

哪里[ANACONDA_FOLDER_PATH]必须替换为 Anaconda 文件夹的完整路径(通常在“程序文件”下)。

What Windows does, when double-clicking on a python script (let's say "file.py"), is pass to SpyderBATCH, as parameter number %1, the full path to "file.py".

当双击 python 脚本(比如“file.py”)时,Windows 所做的是传递给 SpyderBATCH,作为参数 number %1,即“file.py”的完整路径。

Then Spyder is launched and displays the script "file.py" in the editor view.

然后启动 Spyder 并在编辑器视图中显示脚本“file.py”。

回答by Patrick Jilek

I was unable to find a spyder.exe on my installation of conda. However in my users/.anaconda/navigator/scripts I found a spyder.bat file. Using this to open the file opens an anaconda prompt and shortly after spyder will open the file. The file icon is broken but it works for me. Hope this might help.

我在安装 conda 时找不到 spyder.exe。但是在我的 users/.anaconda/navigator/scripts 中我找到了一个 spyder.bat 文件。使用它打开文件会打开 anaconda 提示,不久之后 spyder 将打开文件。文件图标坏了,但对我有用。希望这可能会有所帮助。

回答by JoeB152

I figured I would post my solution for this as well.

我想我也会为此发布我的解决方案。

I have Spyder installed in multiple different environments. You can't simply call the spyder-script.py script without errors, because the environment must be activated.

我在多个不同的环境中安装了 Spyder。你不能简单地调用 spyder-script.py 脚本而不出错,因为环境必须被激活。

@echo off
call [YOUR_CONDA_PATH]\Scripts\activate.bat [YOUR_CONDA_PATH]
call conda activate [YOUR ENVIRONMENT]
call start [YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\pythonw.exe "[YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\Scripts\spyder-script.py" %1

You can remove the second line and remove the environment extension from the third line if you have Spyder installed in your base environment.

如果您在基本环境中安装了 Spyder,则可以删除第二行并从第三行中删除环境扩展。

Hopefully for anyone experiencing any weirdness with the other solution, this one will do the trick by activating the environment correctly.

希望对于其他解决方案有任何奇怪之处的人来说,这个解决方案将通过正确激活环境来解决问题。

回答by Max K.

The solution from JoeB152worked perfectly for me!

JoeB152解决方案非常适合我!

If you are interested in adding the spyder icon (or any other) to the .py-files and if you would like to avoid the cmd-pop-up, I found out the following workaroundwhich is feasible without admin rights:

如果您有兴趣将 spyder 图标(或任何其他图标)添加到 .py 文件,并且您想避免 cmd 弹出窗口,我发现以下解决方法在没有管理员权限的情况下是可行的:

  1. Download the portable version of Bat To Exe Converter(I used v3.0.10).
  2. Open your custom .bat-file in the Bat to Exe Converter.
  3. In the options settings, activate "Icon" and give the path to the respective icon (for me it's in: .../AppData/Local/Continuum/anaconda3/Scripts/spyder.ico).
  4. Set Exe-Formatto Invisible(no empty cmd window would pop up anymore)
  5. Convert your .bat-file to an .exe-file.
  6. As usual, set to open .py-files with the newly created .exe.
  1. 下载Bat To Exe Converter的便携版(我使用的是 v3.0.10)。
  2. 在 Bat to Exe Converter 中打开您的自定义 .bat 文件。
  3. 在选项设置中,激活“图标”并给出相应图标的路径(对我来说它在:).../AppData/Local/Continuum/anaconda3/Scripts/spyder.ico
  4. Exe-Format设置为Invisible(不再弹出空的 cmd 窗口)
  5. 将您的 .bat 文件转换为 .exe 文件。
  6. 像往常一样,设置为使用新创建的 .exe 打开 .py 文件。

Enjoy!

享受!

Environment: Windows 10, Conda 4.8.2, Spyder 4.0.1, Python 3.7

环境:Windows 10、Conda 4.8.2、Spyder 4.0.1、Python 3.7