hoangKnLai / vscode-ipython

VSCode Extension integrating Editor with IPython console.
MIT License
18 stars 5 forks source link

Add option to run file in its directory #51

Closed sevoigt closed 6 months ago

sevoigt commented 6 months ago

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.

hoangKnLai commented 6 months ago

Hi, I'll look into it further and do put in a pull request. Thanks!

hoangKnLai commented 6 months ago

I've review the changes. Here are a few thoughts I'm look into on resolving this issue:

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:

I assume there is a good reason for it, but I would recommend considering safer approach:

hoangKnLai commented 6 months ago

For now, assuming that all these scripts are meant to be independent scripts, consider using dedicated terminal creation command for each script instead:

If this works for you, I'll prioritize implementing a command that create a dedicated terminal with current directory the script's directory.

sevoigt commented 6 months ago

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.

hoangKnLai commented 6 months ago

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"
},
Results Note: - Two terminals both are created in same root folder - One is with current directory the `.py` directory (e.g., error when try to import file not in subfolder) ![image](https://github.com/hoangKnLai/vscode-ipython/assets/64025449/31f74d7c-17c3-4ccb-bb90-c3fc0fea859d)

Next:

sevoigt commented 6 months ago

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?

hoangKnLai commented 6 months ago

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 😃.

hoangKnLai commented 6 months ago

Published changes. Enjoy!