Skip to content

Local imports do not work in native REPL - sys.path differs from cwd than expected  #24361

@ConorTosh

Description

@ConorTosh

Apologies for the mis-label. I indented to submit this issue as a bug, but was having issues with the Issue Reporter is VS Code, and I could not find a way to submit a bug directly via GitHub.

Behaviour

When using the new native REPL, importing from modules in the current workspace directory does not work.

For example, I have created a folder called test which has been opened in VS Code. This has a python file named script.py, which contains the line x=1.
test
├─ script.py

Image

Attempting to import x from the script in the native REPL returns a ModuleNotFoundError.
Image

os.getcwd() shows that the correct working directory has been set. Examining sys.path shows that the current workspace is not present.
Image

Inserting the current workspace into sys.path using sys.path.insert(0, '') allows imports from local files to work as expected.
Image

When using the terminal, imports work as expected, without the need to manually add the workspace directory to sys.path.
Image

This makes the native REPL difficult to use, as you need to type sys.path.insert(0, '') at the beginning of every session, and is not consistent with the behaviour of the terminal.

Steps to reproduce:

  1. Create a python file in the current working directory.
  2. Assign a variable in this script and save it.
  3. Open the native REPL and do from <file> import <variable>. This will fail.
  4. do sys.path.insert(0, '')
  5. Repeat step 3, this will now succeed.

Metadata

Metadata

Assignees

Labels

area-replbugIssue identified by VS Code Team member as probable bugneeds spikeLabel for issues that need investigation before they can be worked on.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions