Skip to content

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

Closed

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.

Activity

  1. anthonykim1 commented on Oct 31, 2024

    @anthonykim1

    Thanks for filing the issue and trying out the new native REPL :)

    Have you set up your workspace in terms of https://code.visualstudio.com/docs/editor/workspaces#_singlefolder-workspace-settings ?
    I wonder if you are on single or multi workspace scenario.

    It would be helpful if you could share directory structure too. There has been work done to set up the cwd for a single workspace directory as the project root itself: #23912 so you might be missing .vscode folder on your directory structure?? I would have to the directory structure honestly.

  2. added
    bugIssue identified by VS Code Team member as probable bug
    and removed
    feature-requestRequest for new features or functionality
    triage-neededNeeds assignment to the proper sub-team
    on Oct 31, 2024
  3. ConorTosh commented on Nov 1, 2024

    @ConorTosh
    Author

    I am using a single workspace setup. In the example, there was no .vscode folder in the directory. Just the script inside the directory.

    test
    ├─ script.py

    I have tried adding a .vscode folder with a blank settings.json like below, but nothing seems to have changed, and the issue still occurs.
    test
    ├─ .vscode
    │ ├─ settings.json
    ├─ script.py

    Here is my user settings.json if this helps

    {
        "terminal.integrated.defaultProfile.windows": "Command Prompt",
        "terminal.integrated.persistentSessionReviveProcess": "never",
        "terminal.integrated.enablePersistentSessions": false,
        "security.workspace.trust.untrustedFiles": "open",
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.removeUnusedImports": "explicit",
            "source.organizeImports": "explicit"
        },
        "workbench.startupEditor": "none",
        "python.defaultInterpreterPath": "C:\\Users\\conor.tosh\\Documents\\envs\\playground\\Scripts\\python.exe",
        "python.languageServer": "Pylance",
        "python.analysis.indexing": true,
        "python.analysis.typeCheckingMode": "off",
        "workbench.editorAssociations": {
            "*.csv": "gc-excelviewer-csv-editor"
        },
        // "python.terminal.launchArgs": [
        //     "-m",
        //     "IPython",
        //     "--no-autoindent",
        // ],
        "git.confirmSync": false,
        "[html]": {
            "editor.defaultFormatter": "vscode.html-language-features"
        },
        "redhat.telemetry.enabled": false,
        "editor.wordWrapColumn": 88,
        "editor.stickyScroll.enabled": true,
        "terminal.integrated.stickyScroll.enabled": true,
        "editor.formatOnPaste": true,
        "cSpell.language": "en-GB",
        "cSpell.diagnosticLevel": "Hint",
        "python.analysis.autoImportCompletions": true,
        "python.analysis.inlayHints.functionReturnTypes": true,
        "python.analysis.autoFormatStrings": true,
        "python.venvPath": "C:\\Users\\conor.tosh\\Documents\\envs",
        "workbench.editor.enablePreview": false,
        "jupyter.askForKernelRestart": false,
        "SAS.connectionProfiles": {
            "activeProfile": "",
            "profiles": {}
        },
        "python.analysis.inlayHints.pytestParameters": true,
        "debugpy.debugJustMyCode": false,
        "editor.accessibilitySupport": "off",
        "jupyter.notebookFileRoot": "${workspaceFolder}",
        // "jupyter.interactiveWindow.textEditor.executeSelection": true,
    }
    
  4. github-actions commented on Dec 2, 2024

    @github-actions

    Hey Anthony Kim (@anthonykim1), this issue might need further attention.

    ConorTosh, you can help us out by closing this issue if the problem no longer exists, or adding more information.

  5. ConorTosh commented on Dec 2, 2024

    @ConorTosh
    Author

    The issue still exists. Please let me know what further information is needed.

  6. hutch3232 commented on Dec 7, 2024

    @hutch3232

    I have the same issue as ConorTosh.

    Printing sys.path in the native REPL vs. terminal REPL:

    # native repl
    ['c:\\Users\\hutch3232\\.vscode\\extensions\\ms-python.python-2024.20.0-win32-x64\\python_files', 'C:\\Users\\hutch3232\\AppData\\Local\\Programs\\Python\\Python313\\python313.zip', 'C:\\Users\\hutch3232\\AppData\\Local\\Programs\\Python\\Python313\\DLLs', 'C:\\Users\\hutch3232\\AppData\\Local\\Programs\\Python\\Python313\\Lib', 'C:\\Users\\hutch3232\\AppData\\Local\\Programs\\Python\\Python313', 'c:\\Users\\hutch3232\\Documents\\python\\finsim\\.venv', 'c:\\Users\\hutch3232\\Documents\\python\\finsim\\.venv\\Lib\\site-packages']
    
    # terminal REPL
    ['', 'C:\\Users\\hutch3232\\AppData\\Local\\Programs\\Python\\Python313\\python313.zip', 'C:\\Users\\hutch3232\\AppData\\Local\\Programs\\Python\\Python313\\DLLs', 'C:\\Users\\hutch3232\\AppData\\Local\\Programs\\Python\\Python313\\Lib', 'C:\\Users\\hutch3232\\AppData\\Local\\Programs\\Python\\Python313', 'C:\\Users\\hutch3232\\Documents\\python\\finsim\\.venv', 'C:\\Users\\hutch3232\\Documents\\python\\finsim\\.venv\\Lib\\site-packages']
    

    I opened this project as a Folder. By going File > Open Folder... and selecting this directory: C:\Users\hutch3232\Documents\python\finsim

    I don't think this is using a Workspace - I don't have a .vscode folder in this project. Tested on python 3.12.8 as well. This definitely was working for me not that long ago.

  7. anthonykim1 commented on Dec 9, 2024

    @anthonykim1

    Thanks for the directory structure ConorTosh , I'm going to update the label to be more appropriate to needing spike.

  8. 9 remaining items

  9. simonh-tecex commented on Jul 25, 2025

    @simonh-tecex

    I am experiencing the same issue. Upvote for a fix! Thank you!

    import os
    os.getcwd()
    

    Shows that the Native REPL window CWD is indeed my project/ workspace root folder.

    import sys
    sys.executable
    

    Shows that the Native REPL window python.exe is indeed the one from my projects .venv folder.

    However, performing a local import of a function from a .py file and folder within my local workspace fails "out the box" with the error: ModuleNotFoundError: No module named ...

    This is after the type hinting/ autocomplete recommended that the import would work!

    The fix recommended above, to run this command:

    sys.path.insert(0,'')

    does work.

    after running sys.path.insert(0,'') the local project imports work as expected! Thanks for that!

  10. removed
    needs spikeLabel for issues that need investigation before they can be worked on.
    on Sep 9, 2025
  11. added a commit that references this issue on Sep 9, 2025
  12. anthonykim1 commented on Sep 11, 2025

    @anthonykim1

    Sorry for long wait folks, please try out the pre-release version of Python extension with vscode insiders: https://code.visualstudio.com/insiders/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-replbugIssue identified by VS Code Team member as probable bugverifiedVerification succeeded

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions