Skip to content

Run Python File In Terminal doesn't escape special chars in pathnames #4165

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

Closed
lockejan opened this issue Jan 28, 2019 · 5 comments
Closed
Labels
area-environments Features relating to handling interpreter environments area-terminal bug Issue identified by VS Code Team member as probable bug linux macos needs PR Ready to be worked on

Comments

@lockejan
Copy link

lockejan commented Jan 28, 2019

Environment data

  • VS Code version: 1.30.2
  • Extension version (available under the Extensions sidebar): 2018.12.1
  • OS and version: macOS 10.14.2
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.2 64-bit
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): N/A
  • Relevant/affected Python packages and their versions: XXX

Expected behaviour

Running a script via Terminal would execute no matter where inside the project directory the script is stored

Actual behaviour

filepathes can't be resolved due to lack of escaping special characters:

example for special characters in pathname:
image
image

Steps to reproduce:

  1. Create a subdirectory in your project folder having brackets in it's name, for instance "foo(bar)".
  2. Create a file inside that directory. (In my case foo.py)
  3. Open the main project folder via the File-Handler in VSC.
  4. Open the foo.py file inside the subdirectory and execute it via "Run Python File In Terminal"

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

Starting Jedi Python language engine.
##########Linting Output - pylint##########

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help; turn on source maps to make any tracebacks be useful by running Enable source map support for extension debugging)

XXX 

Old Second Part of Issue

Expected behaviour

linked path resources would be resolved relatively to the directory of the executed script

Actual behaviour

example for non resolvable pathes inside scripts:
image
image

line 3 of the script:
image

Steps to reproduce:

  1. Create a subdirectory in your project folder having brackets in it's name, for instance "foo(bar)".
  2. Create a file inside that directory. (In my case foo.py)
  3. Open the main project folder via the File-Handler in VSC.
  4. Open the foo.py file inside the subdirectory and execute it via "Run Python File In Terminal"
  5. Terminal replies then with "FileNotFoundError: [Errno 2] No such file or directory:"
@ghost ghost added the triage-needed Needs assignment to the proper sub-team label Jan 28, 2019
@lockejan lockejan changed the title Run Python File In Terminal crash Run Python File In Terminal doesn't escape special chars in pathnames nor resolve linked files inside scripts Jan 28, 2019
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Jan 28, 2019
@ericsnowcurrently
Copy link

Thanks for letting us know about this, @lockejan. There are two separate issues at play here:

  1. special characters in filenames are interpreted by the shell
  2. scripts are executed relative to the project root

(In the future you'll be best served by opening separate issues.)

For the first one that seems like something we could address (e.g. quote the entire filename when passing to the shell). I expect we haven't done so because having such characters in your file/directory names is pretty uncommon. Regardless, we'll look into it.

@ericsnowcurrently
Copy link

As to the second issue (CWD for running scripts), that's mostly a function of how Python executes scripts. It does not modify the current working directory to match that of the script. Python does, however, put the script's directory at the front of sys.path.

The typical approach for data files in Python is to resolve the absolute path relative to the script:

import os.path

DATADIR = os.path.dirname(os.path.abspath(__file__))
DATAFILE = os.path.join(DATADIR, 'fighter.csv')

...

You are welcome to submit a feature request to have "Run Python File In Terminal" change to the script's directory before running (perhaps as a setting-enabled option). However, it's unlikely that we'd do something like that.

@ericsnowcurrently ericsnowcurrently added bug Issue identified by VS Code Team member as probable bug area-terminal needs decision and removed triage labels Feb 4, 2019
@ericsnowcurrently
Copy link

I've marked this as a bug since I'd expect us to be quoting filenames in the terminal.

@ericsnowcurrently ericsnowcurrently removed their assignment Feb 4, 2019
@ericsnowcurrently ericsnowcurrently changed the title Run Python File In Terminal doesn't escape special chars in pathnames nor resolve linked files inside scripts Run Python File In Terminal doesn't escape special chars in pathnames Feb 4, 2019
@gramster gramster added the area-environments Features relating to handling interpreter environments label Oct 10, 2019
@brettcannon
Copy link
Member

This isn't an issue under Windows, so marking as a macOS/Linux issue.

@karrtikr
Copy link

Closing as dup of #20360.

@karrtikr karrtikr closed this as not planned Won't fix, can't repro, duplicate, stale Dec 14, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-environments Features relating to handling interpreter environments area-terminal bug Issue identified by VS Code Team member as probable bug linux macos needs PR Ready to be worked on
Projects
None yet
Development

No branches or pull requests

7 participants