Closed sevoigt closed 6 months ago
Hi, I'll look into it further and do put in a pull request. Thanks!
I've review the changes. Here are a few thoughts I'm look into on resolving this issue:
runFileInItsDir
command which anyone with this specific use case can map it to their prefer keybind
%cd
when terminal is already in directory.cdFileDir
command to change terminal directory to file's directory (most of the time, it is likely the case that user would only change directory once, run one or more files, then cd back or to another directory)
cdWorkspace
command to change terminal directory to vscode
workspace directory (effectively undoing %cd
called)On a side note and more of an opinion: this directory feature, though convenient is likely not a very safe practice since changing terminal current directory often changes its behaviors:
autoreload
when cd
to new directory may find conflicts between namespace in prior directory and likely will have trouble resolving the conflict automatically. This can cause unintended behaviors / bug that can be very difficult to resolve. I assume there is a good reason for it, but I would recommend considering safer approach:
PYTHONPATH
along with vscode:python
environment setting For now, assuming that all these scripts are meant to be independent scripts, consider using dedicated terminal creation command for each script instead:
ctrl+shift+i F5
%cd
to script folder. From then on, every time the script is ran with F5
it will use that specific terminal with the script directory.If this works for you, I'll prioritize implementing a command that create a dedicated terminal with current directory the script's directory.
Open a script, create a new script dedicated terminal with ctrl+shift+i F5 This likely to produce an error if the script is not in scope / PYTHONPATH but ignore that as long as the IPython terminal is created successfully Manually %cd to script folder. From then on, every time the script is ran with F5 it will use that specific terminal with the script directory.
This is what I currently do, just tried to get rid of the manually %cd. A separate command would be fine, I get the benefits.
And your're right, the scripts are standalone/independent and if they would not need to load/save external files with relative paths this would not be an issue at all. However I can actually not think of a case where I would not run a script from its directory. Because this is the only way to have a consistent environment. Otherwise the script behaviour would depend on the editor/IDE/shell it is run from (and their working dir settings) which is totally undesirable.
Progress:
{
"category": "IPython",
"command": "ipython.createDedicatedTerminal",
"title": "Create a Dedicated IPython Terminal for Current Active Python File"
},
{
"category": "IPython",
"command": "ipython.createDedicatedTerminalInFileDir",
"title": "Create a Dedicated IPython Terminal with its Current Directory the Active Python File Directory"
},
Next:
ctrl+shift+i shift+c
and ctrl+shift+i ctrl+shift+c
Sorry for the late reply... I checked out the branch and it works for me. Personally I would probably mostly use createDedicatedTerminalInFileDir. The other command seems to be similar to ipython.runFileInDedicatedTerminal, maybe that's sufficient already?
No problem at all. Thanks for testing it out and for creating this issue. This weekend, I can close #52, publish the updates, and PR this issue branch.
The other command seems to be similar to ipython.runFileInDedicatedTerminal, maybe that's sufficient already?
ipython.createDedicatedTerminal
command currently does not have a %cd
option. So yes, the createDedicatedTerminalInFileDir
is specifically added to address this issue 😃.
Published changes. Enjoy!
The default working directory of the IPython shell seems to be the directory opened in VS code.
Since I often use relative paths in python scripts to load and save data, this breaks scripts in subdirectories or requires manual change of the working directory each time.
Thus an option would be nice to cd into the script's directory automatically when running a script. This also is the default behaviour of some other Python IDEs I've used (Spyder, Pyscripter)
I've already done this and could create a pull request if you like. It's just a %cd before each %run and a setting to toggle it on/off.