Thanks to visit codestin.com
Credit goes to github.com

Skip to content

"Edit with Spyder" should set cwd to .py file directory #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nerdfever opened this issue May 6, 2019 · 17 comments
Open

"Edit with Spyder" should set cwd to .py file directory #748

nerdfever opened this issue May 6, 2019 · 17 comments

Comments

@nerdfever
Copy link

nerdfever commented May 6, 2019

Problem Description

When you right-click on a .py file and choose "Edit with Spyder", Spyder starts up with the working directory (os.getcwd) set to the Spyder settings directory.

In Preferences you can change this (I think) to one specific directory of choice, but only one.

When starting Spyder this way, the cwd should be set to the location of the ,py file.

(At least, that should be one of the options in Preferences.)

What steps reproduce the problem?

  • Install WinPython
  • Right-click on a .py file
  • Choose "Edit with Spyder".
  • Observe resulting os.getcwd()

What is the expected output? What do you see instead?

cwd should be location of .py file.

Spyder version: 3.3.1
Python version: 3.7
Operating System name/version: Win 10 Pro x64

@nerdfever nerdfever changed the title "Edit with Spyder" (Windows) should set cwd to .py file directory "Edit with Spyder" should set cwd to .py file directory May 6, 2019
@stonebig
Copy link
Contributor

stonebig commented May 6, 2019

well, if you know how to tweak the code here, I'll be interesting to see your pull request.

https://github.com/winpython/winpython/blob/master/winpython/associate.py#L74..L98

maybe it's rather a Spyder problem, or another entry to add "edit wiith Spyder in this directory"

@nerdfever
Copy link
Author

nerdfever commented May 17, 2019

I've discovered that changing the way "Edit with Spyder" is setup in the registry almost does what I want:

In:
Computer\HKEY_CURRENT_USER\Software\Classes\Python.File\shell\Edit with Spyder\command
and:
Computer\HKEY_CURRENT_USER\Software\Classes\Python.NoConFile\shell\Edit with Spyder\command

(No, I don't know why this is in two places; I'm no expert on the Windows registry.)

If the default entry there is changed to:

C:\WPy-3702\Spyder.exe "%~1" -w "%~p1"

That almost makes Spyder set the working directory to the location of the .py file.

The problem is that %~p1 returns a path with a trailing \, which seems to confuse Spyder. Without the trailing \, this works.

@stonebig
Copy link
Contributor

stonebig commented May 17, 2019

Maybe add ".." ? Or "." ? ~p1.

@nerdfever
Copy link
Author

nerdfever commented May 17, 2019

That would change the front of the string, but not the end, which is where the problem is.

I submitted a Spyder issue. See spyder-ide/spyder#9354

I suppose I could write a batch file that called a Python script that pre-processed the command line. But that really shouldn't be necessary. (And it would make Spyder load even slower.)

@stonebig
Copy link
Contributor

stonebig commented May 17, 2019

C:\WinP\bd37\bu\WPy64-3730b3\scripts\spyder.bat C:\WinP\bd37\bups2\WPy64-3730b3\notebooks\docs\minesweeper.py -w "C:\WinP\bd37\bups2\WPy64-3730b3\notebooks\docs\." seems to work

@nerdfever
Copy link
Author

nerdfever commented May 17, 2019

@stonebig I think I misunderstood you. I just tried adding "." at the end of the path string.

Yes, it seems to work to add a "." to the end of the path on the command line.

But it doesn't work in the registry. I don't know why. I tried the dot on the other side of the closing quote, too. Still doesn't work.

image

@stonebig
Copy link
Contributor

stonebig commented May 17, 2019

and with "%~p1\" ? as C:\WinP\bd37\bu\WPy64-3730b3\scripts\spyder.bat C:\WinP\bd37\bups2\WPy64-3730b3\notebooks\docs\minesweeper.py -w "C:\WinP\bd37\bups2\WPy64-3730b3\notebooks\docs\\" seems to work also

@nerdfever
Copy link
Author

Tried "%~p1\" (thanks). No joy. Also tried "%~p1\."

Both work in the command line, neither in the registry. Don't know why.

Is there a way to see what command line Spyder thinks it got?

@stonebig
Copy link
Contributor

stonebig commented May 17, 2019

not a specialist for, and with "%~p1/" ? "%~p1 " ?

@stonebig
Copy link
Contributor

seems there is no solution : even if you type the directory the "hard way" in the registery, it just fails to launch spyder.

@stonebig
Copy link
Contributor

so there are a bit of research/experiment to do to figure it out.

@e-d-n-a
Copy link

e-d-n-a commented Feb 10, 2021

Sorry, a bit late (maybe it still helps someone xD) :

The parameter-syntax is actually different for registry shell commands and the CMD shell!
I see, you expected to be able to use the" CMD shell"-syntax like "%~1" or even "%~p1", but both are actually invalid (afaik).
I tested it thoroughly with some tricks (with WinPython-x64-3870) and both forms produce no output.

Searching for actually available syntax lead me to this thread:
https://superuser.com/questions/136838/which-special-variables-are-available-when-writing-a-shell-command-for-a-context
with the original information source:
https://www.robvanderwoude.com/ntstart.php

which contains a list of valid options:

    %* – Replace with all parameters.

    %~ – Replace with all parameters starting with and following the second parameter.

    %0 or %1 – The first file parameter. For example "C:\Users\Eric\Desktop\New Text Document.txt". Generally this should be in quotes and the applications command line parsing should accept quotes to disambiguate files with spaces in the name and different command line parameters (this is a security best practice and I believe mentioned in MSDN).

    %<n> (where <n> is 2-9) – Replace with the nth parameter.

    %s – Show command.

    %h – Hotkey value.

    %i – IDList stored in a shared memory handle is passed here.

    %l – Long file name form of the first parameter. Note that Win32/64 applications will be passed the long file name, whereas Win16 applications get the short file name. Specifying %l is preferred as it avoids the need to probe for the application type.

    %d – Desktop absolute parsing name of the first parameter (for items that don't have file system paths).

    %v – For verbs that are none implies all. If there is no parameter passed this is the working directory.

    %w – The working directory.

So I simply tested the "%w"-parameter and it worked perfectly:

"C:\WinPython\WPy-x64-3870\spyder.exe" "%1" -w "%w"

I tested in Win7, but I believe it will be the same in Win10.

@stonebig
Copy link
Contributor

found this https://stackoverflow.com/questions/659647/how-to-get-folder-path-from-file-path-with-cmd

:\Temp>batchparams.bat c:\windows\notepad.exe
%~1     =      c:\windows\notepad.exe
%~f1     =      c:\WINDOWS\NOTEPAD.EXE
%~d1     =      c:
%~p1     =      \WINDOWS\
%~n1     =      NOTEPAD
%~x1     =      .EXE
%~s1     =      c:\WINDOWS\NOTEPAD.EXE
%~a1     =      --a------
%~t1     =      08/25/2005 01:50 AM
%~z1     =      17920
%~$PATHATH:1     =
%~dp1     =      c:\WINDOWS\
%~nx1     =      NOTEPAD.EXE
%~dp$PATH:1     =      c:\WINDOWS\
%~ftza1     =      --a------ 08/25/2005 01:50 AM 17920 c:\WINDOWS\NOTEPAD.EXE

@stonebig
Copy link
Contributor

stonebig commented Feb 13, 2021

I'll change the spyder.bat scripts to this:

  • if nothing (click on classic icon), make spyder icon point to classic notebook directory
  • if drag & drop of a file, open spyder on the directory of that file
  • if launching from a directory, open on that directory

Does it fit the general purpose ?

@echo off
set directory_of_parameter1=%~dp1
if "%directory_of_parameter1%"=="" (
   if not "%CD%\"=="%~dp0" set  directory_of_parameter1=%CD%
)
call "%~dp0env_for_icons.bat"
if "%directory_of_parameter1%"=="" set directory_of_parameter1=%WINPYWORKDIR%
rem cd/D "%WINPYWORKDIR%"
if exist "%WINPYDIR%\scripts\spyder3.exe" (
   "%WINPYDIR%\scripts\spyder3.exe" %* -w "%directory_of_parameter1%"
) else (
   "%WINPYDIR%\scripts\spyder.exe" %* -w "%directory_of_parameter1%"
)   

@nerdfever
Copy link
Author

Sounds good.

@e-d-n-a
Copy link

e-d-n-a commented Feb 13, 2021

If you want to solve it via a CMD batch file, then the parameter syntax is documented with the "for"-command:
Checkout all available options at the end of "for /?".

@stonebig
Copy link
Contributor

stonebig commented May 4, 2025

oups, is there something to tweak in the more readable associate.py ?
https://github.com/winpython/winpython/blob/master/winpython/associate.py#L136

@stonebig stonebig added this to the 2025-03 PEP 751 / AI local milestone May 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants