microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.34k stars 1.19k forks source link

Local imports do not work in native REPL #24361

Open ConorTosh opened 4 weeks ago

ConorTosh commented 4 weeks ago

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.
anthonykim1 commented 3 weeks ago

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: https://github.com/microsoft/vscode-python/pull/23912 so you might be missing .vscode folder on your directory structure?? I would have to the directory structure honestly.

ConorTosh commented 3 weeks ago

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,
}