Skip to content
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

Python cells are evaluated in the project or current working directory #3156

Open
gadenbuie opened this issue May 15, 2024 · 8 comments
Open
Labels
area: code cells Issues related to the Code Cells extension enhancement New feature or request lang: python

Comments

@gadenbuie
Copy link

This is related to #3155 but might be considered a bug, so I'm opening a separate issue.

Positron Version:

Positron Version: 2024.05.0 (Universal) build 1251
Code - OSS Version: 1.89.0
Commit: c0d8830
Date: 2024-05-15T13:58:23.111Z
Electron: 28.2.8
Chromium: 120.0.6099.291
Node.js: 18.18.2
V8: 12.0.267.19-electron.0
OS: Darwin arm64 23.4.0

Steps to reproduce the issue:

Create a project with the following structure:

.
└── scripts
    ├── _one.py
    └── main.py
# scripts/_one.py
def hello_world():
    return "Hello, World!"
# scripts/main.py

# %%
from _one import hello_world
hello_world()

In main.py, click "Run Cell` above the import statement. The sends the code to the Python Console, resulting in a ModuleNotFoundError:

>>> from _one import hello_world
... hello_world()
ModuleNotFoundError: No module named '_one'
Cell In[1], line 1
----> 1 from _one import hello_world
      2 hello_world()

image

What did you expect to happen?

In VS Code, with the Jupyter extension, clicking on Run Cell sends the code to an interpreter (that yes, is tied to the script) where the code is evaluated in the script's parent directory.

image

Were there any error messages in the output or Developer Tools console?

N/A

@gadenbuie gadenbuie added the bug Something isn't working label May 15, 2024
@juliasilge
Copy link
Contributor

For Jupyter notebooks, we use the notebook's location as the working directory as of #2648.

For regular Python scripts, I believe the current behavior is as intended.

These scripts with code cells are kind of a special case. @seeM do you think we should make those behave more like full fledged notebooks?

@seeM
Copy link
Contributor

seeM commented May 16, 2024

This is tricky! There are two parts:

  1. Allow creating a console session with a specified working directory, as we do for notebooks. That part's easy.
  2. Tie each %% script's session to its corresponding file, and manage all of those sessions (as we currently do for notebooks in the positron-notebook-controllers extension) e.g. executing a cell should start a session if one doesn't exist, bring the corresponding script's console to the foreground if it isn't, and so on.

Point 2 might need a fair bit of rewiring since we currently assume that there's only one console session per language.

I don't think we can reuse notebook sessions for that since notebook sessions have different requirements e.g. they're intentionally disconnected from the console and plots panes since notebooks have their own UI for those, the matplotlib backend is also intentionally different, and I'm sure there's more.

Maybe the current console session type is more of a "global console", and we could introduce a new "script console" session type for these?

We'd need to iron out a bunch of details about the UX, like kernel selection, and navigating between consoles for the same language, which we haven't had to deal with yet.

I'm curious to hear @jmcphers' thoughts too.

@jmcphers
Copy link
Collaborator

I don't thing we should start entirely new sessions for these cells, because of all the complications @seeM pointed out. We really don't want a proliferation of sessions if we can avoid it.

What if we did something as simple as just changing the working directory when you run a code cell from a script?

@seeM
Copy link
Contributor

seeM commented May 20, 2024

Maybe #3188 is an even neater workaround?

It would be more explicit than automatically changing the directory when running a code cell, which I think may be confusing for some users.

@seeM seeM self-assigned this May 23, 2024
@juliasilge juliasilge removed the bug Something isn't working label May 28, 2024
@juliasilge juliasilge added this to the Release Candidate milestone May 28, 2024
@seeM seeM removed their assignment Jul 17, 2024
@jthomasmock
Copy link
Contributor

Also relevant: microsoft/vscode-python#23656 (comment)

@juliasilge juliasilge added area: code cells Issues related to the Code Cells extension enhancement New feature or request labels Aug 28, 2024
@jthomasmock
Copy link
Contributor

This might be a good candidate for the new editor UI (toggle between project/current working directory).

@juliasilge
Copy link
Contributor

One option to consider is waiting on this until we have real multiple consoles as tracked in #3173; evaluating a code cell could spawn a new console in the directory of the .py file.

@juliasilge
Copy link
Contributor

We could also optionally provide this kind of functionality (spawn a new console in the file's directory) for .qmd and .Rmd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: code cells Issues related to the Code Cells extension enhancement New feature or request lang: python
Projects
None yet
Development

No branches or pull requests

5 participants