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

48,129

Solution 1

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.

Solution 2

(Spyder maintainer here) This functionality is available as part of our Windows installer. In other words, if you install Spyder with it, then you'll see a new entry in the Open with menu of the Windows Explorer that allows you to open Python files directly on Spyder.

Unfortunately, it's not possible for us to do the same for other installation methods (i.e. when using Anaconda or pip). That's why need to implement hacks as the ones mentioned in other answers here.

Solution 3

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.py via the Open with dialog. If you now try to open your File.py by double clicking you'll receive an error like

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

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

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" %1 with "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.py should now execute in a python shell.

From the docstring of ftype,

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

Solution 4

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.

@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.

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

Solution 5

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

[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").

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".

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

Share:
48,129
Admin
Author by

Admin

Updated on February 21, 2022

Comments

  • Admin
    Admin about 2 years

    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?