question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't run files with space in path via external terminal

See original GitHub issue

I dumped the cmd being called from spyder.util.programs (line 764ish) with a simple print statement, and nothing seems immediately wrong, but [WinError 267] The directory name is invalid is consistently thrown.

Cmd:

start cmd.exe /K “cd “Y:\Users\aaron\python scripts” && “C:\Users\aaron\python\py39\python.exe” -i “Y:\Users\aaron\python scripts\helloworld.py”” ^&^& exit

I can even separately copy and paste the command into a different terminal (just python inside cmd; no IDE) and call the following without issue:

from subprocess import Popen
Popen(r'start cmd.exe /K "cd "Y:\Users\aaron\python scripts" && "C:\Users\aaron\python\py39\python.exe" -i "Y:\Users\aaron\python scripts\helloworld.py"" ^&^& exit', shell=True)

Including the cwd arg does not make a difference either when using Popen myself, but if I modify spyder.utils.programs.py at line 762 to be:

        try:
            # if wdir:
            #     run_shell_command(cmd, cwd=wdir)
            # else:
                run_shell_command(cmd)

the problem is solved.

Am I missing something, or does the “cd” command included in the cmd string take care of not needing the cwd argument? I also cannot figure out why it’s an issue for spyder to include cwd but not when I do it in a terminal myself…

  • Spyder version: 5.3.0 None
  • Python version: 3.9.6 64-bit
  • Qt version: 5.12.10
  • PyQt5 version: 5.12.3
  • Operating System: Windows 10

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ccordoba12commented, Apr 4, 2022

@mrclary, could you give us a hand with this one, since it seems you understand well what needs to be done? Thanks!

0reactions
mrclarycommented, Apr 4, 2022

here wdir is not a quoted string.

Isn’t it? The quotes get added before the OS switching at line 737

You are correct. I miss-read the code.

Will it work on windows if there are spaces in wdir but we don’t quote it? i.e. cwd='path with space'

I believe it is required to be un-quoted when it is passed as the cwd argument to Popen (inside run_shell_command) at least on Windows, however it seems like Linux and MacOS scripts may still need wdir to be quoted because they are used to build shell commands as strings / shell script. I don’t have linux on any of my computers, but I think it needs to be tested.

The linux logic could possibly be simplified as it seems specifying the "wdir-option" in the command args as well as passing wdir to run_shell_command seems redundant like the Windows case.

Yes, I think it most pythonic to use the cwd keyword argument wherever possible; it likely requires unquoted string for all platforms, but should be verified. For linux, then, wdir-option could be removed and cd should be removed from both nt and darwin, with cwd added to the darwin case. The temporary file may not be necessary for darwin either, but I’d have to verify the interaction of Terminal.app with the provided env keyword for Popen

Read more comments on GitHub >

github_iconTop Results From Across the Web

cmd - How to run an executable that contains a space in path ...
Short answer: Use & 'C:\path with spaces\app.exe'. Explanation: Just type your path into powershell and use TAB for auto completion when you ...
Read more >
How to call a program that contains space in filename?
This answer is only a partial solution: It will work if there are spaces in the path but it will not work if...
Read more >
Cannot open a file with a space in it's filename using cmd.exe ...
cmd.exe /C start test-no-spaces.txt; Attempt to open "test spaces.txt" with the default Windows application using
Read more >
How do I navigate to folders with spaces in their names? I get ...
Go inside the /opt directory via terminal and then run below command, k@k-Ubuntu:~$ cd /opt k@k-Ubuntu:/opt$ cd "Sublime Text 2".
Read more >
5 Ways to Fix the "Not Recognized as an Internal or External ...
To resolve this error, check if the commands are correct and use file paths with spaces within double-quotes. For the programs not installed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found