-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Added setting to execute files in terminal using the file's directory… #316
Conversation
… as the working directory
@@ -38,8 +40,13 @@ function execInTerminal(fileUri?: vscode.Uri) { | |||
filePath = `"${filePath}"`; | |||
} | |||
const terminal = vscode.window.createTerminal(`Python`); | |||
if (pythonSettings.terminal.executeInFileDir) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gandhis1 , this line could fall over if such a setting doesn't exist.
You might want to check if it eixsts as follows (or similar way):
if (pythonSettings.termal && pythonSettings.terminal.executeInFileDir)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made this change (pushing shortly), but I'm unclear on how exactly that would be possible. The presence of a default value for this setting does not preclude it from not existing?
Added another setting to support #321. Also, I realized that the terminal does not show and focus when it is run, which leads to awkward situations where a user might not realize they already ran the "Run File" command, when it is hidden, or hidden behind another terminal instance. So I made the terminal appear following the command. Questions:
|
} | ||
const launchArgs = settings.PythonSettings.getInstance().terminal.launchArgs; | ||
const launchArgsString = launchArgs.length > 0 ? " ".concat(launchArgs.join(" ")) : ""; | ||
terminal.sendText(`${currentPythonPath} ${filePath}${launchArgsString}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on #321, shouldn't the order of the command be as follows?
As we're trying to pass arguments into the python interpreter and not the program.
terminal.sendText(`${currentPythonPath}${launchArgsString} ${filePath}`);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thanks for spotting my crappy code. I actually did test the code, and incorrectly figured that it was normal behavior for the custom arguments to print nothing to stdout. :-/
Yes
Whether you provide the absolute or relative path, its still the same, isn't it? So using absolute paths doesn't matter. |
What is a valid hypothetical use case for the setting? If you run a Python script normally, it will still be able to access relative paths if you use relative imports and an init.py, which correct me if I'm wrong, but operate off of the file path, rather than the current working directory. Why does the working directory matter? I can't see a robust use case that wouldn't be better addressed by setting PYTHONPATH instead. |
@gandhis1 , I'm confused. Are these new questions related to the execution of selected code or applies to executeInFileDir as well?
Doesn't the working directory matter if you have code that for some reason access the current working directory. E.g. code using
See previous answer |
I'm talking about either run type. Yes, if you use os.getcwd(), it matters. But if you're using os.getcwd() to access resources under the assumption that your working directory = file directory, you have now precluded your script from working under any other execution context. What if you later you turn your script into a module, which is imported from somewhere else, with no context guarantees? I'm throwing it out there that use cases for this might be better served by using |
@gandhis1 , these are valid questions. However didn't you already start this PR based on some assumptions? What were they? Why start the PR with the ability to set the current working directory? Also, isn't #314 a valid user case for this requirement? |
I wanted to continue the discussion on this and get some feedback from other experienced Python users on the pros/cons of this change. I'm totally OK with this PR getting killed (the executeInFileDir part, not the LaunchArgs part)...just want to hear some reasoning by others, who might have different work contexts (I'm a finance quant). #314 explains what, but not why. A lot of people are going to request features that may not make sense or lacks an understanding of the bigger picture of why a feature exists and what the appropriate usage case for a given feature is. I suspect this might be one... From my original post: |
@gandhis1, agreed a discussion is warranted. |
@gandhis1, sorry if I sounded rude. Just thought had some other scenarios that warranted the PR. If you're happy with the changes, I will merge them, let me know when. |
Refactor how environment variables are parsed and used in tools, language server and debugger Fixes #316 (PYTHONPATH not used as extra path for auto complete) Fixes #183 (changes to .env file requires restarts of vscode) Fixes #436 (Path variables not inherited when debugging) Fixes #435 (Virtual environment name is same as the default environment file)
… as the working directory.
Couple of caveats:
This change addresses #314 , by giving the user a setting of "python.terminal.executeInFileDir", defaulting to False, that, when set to True, will navigate to the directory of the file first, before executing the file in the terminal.