pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

VS Code Python Issue: "Default Python Functions Not Defined" Error

Open john4dragon opened this issue 1 year ago • 10 comments

Description of the Problem:

I am experiencing an error in Visual Studio Code across all default Python functions (e.g., input(), print(), etc.). When trying to use these functions in a Python script, I receive errors stating that these functions are "not defined".

  • Environment Details:

Operating System (macOS Monterey ver. 12.7.2): Visual Studio Code Version (1.86.0): Python Extension Version in VS Code (2024.0.1): Python Version (Python 3.11.5, All Versions I have installed): Any other relevant extensions or tools installed (if any): I am using Anaconda3 I have no extensions other than the default Python, Python Debugger, and Pylance

Steps to Reproduce:

  1. Open a new or existing Python file in VS Code.
  2. Write simple Python code using standard functions, such as print("Hello World") or name = input("Enter your name: ").
  • Expected Behavior:

The script should execute without errors, and standard Python functions should work as expected. Pylance should not flag default Python functions as reportUndefinedVariable.

  • Actual Behavior:

When running the script it executes normally and runs as normal, but VS Code flags errors for all standard Python functions, claiming they are "not defined". This is occurring despite these functions being integral parts of Python.

  • Troubleshooting Steps Already Taken:
  1. Ensured the correct Python interpreter is selected in VS Code.
  2. Reinstalled the Python extension in VS Code.
  3. Checked settings.json for any potentially conflicting settings, but I do not know enough to know if there are any conflicts.
  4. Updated VS Code and all extensions to the latest versions.
  5. Deleted and Reinstalled VS Code
  • Log when running a hello world file

output.txt

Normal Hello World Script like as follows has the following problem, the function print is underlined in yellow. print('Hello, World!') In the problems tab the following error occurs

"print" is not defined Pylance(reportUndefinedVariable) [Ln 1, Col 1] VSCode_Issue1

VSCode_Issue2

Additional Context:

The issue seems to be isolated to Visual Studio Code, I suspect it might be related to Pylance, Because when pylance is disabled the issue goes away. Other QOL enhancements do too, but the original issue also disappears. I would also like to add, I am pretty new to learning python and am not sure how this occurred, so I'm not exactly sure how to replicate this issue. If i end up finding a solution i will add it.

Edit 2/6/2024 7:31pm EST : I managed to fix the issue. I completely uninstalled VSCode and all associated files and settings. I redownloaded it and reinstalled my plugins and it works fine. I still have no idea what caused this issue, but if it happens to anyone else my only advice is to completely delete and reinstall vscode, deleting the following files as well before you reinstall. ~/.config/Code ~/Library/Application Support/Code ~/Library/Caches/com.microsoft.VSCode ~/Library/Caches/com.microsoft.VSCode.ShipIt ~/Library/Preferences/com.microsoft.VSCode.helper.plist ~/Library/Preferences/com.microsoft.VSCode.plist ~/Library/Saved Application State/com.microsoft.VSCode.savedState

john4dragon avatar Feb 06 '24 23:02 john4dragon

Can you please provide a log as described under "Filing an issue" in the troubleshooting guide?

debonte avatar Feb 07 '24 00:02 debonte

Can you please provide a log as described under "Filing an issue" in the troubleshooting guide?

I have added it into the file output.txt

ghost avatar Feb 07 '24 00:02 ghost

This is weird. Any chance you have typeshed installed in your conda environment? Asking because I searched through our old issues for Could not import 'builtins' and the only hit was an issue caused by installing typeshed. https://github.com/microsoft/pylance-release/issues/4508#issuecomment-1599448940

2024-02-06 18:12:31.637 [info] [Info  - 6:12:31 PM] (31127) Could not import 'builtins' in file '/Users/jdd31/Desktop/Python Code/CS103/prob1.py'
2024-02-06 18:12:31.639 [info] [Info  - 6:12:31 PM] (31127)   Looking in stubPath '/Users/jdd31/Desktop/Python Code/typings'
2024-02-06 18:12:31.641 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/Desktop/Python Code/typings'
2024-02-06 18:12:31.641 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/Desktop/Python Code/typings'
2024-02-06 18:12:31.642 [info] [Info  - 6:12:31 PM] (31127)   Looking in root directory of execution environment '/Users/jdd31/Desktop/Python Code'
2024-02-06 18:12:31.642 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/Desktop/Python Code'
2024-02-06 18:12:31.643 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/Desktop/Python Code'
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)   Finding python search paths
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)   Executing interpreter: '/Users/jdd31/anaconda3/bin/python'
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)   Skipping '/Users/jdd31/anaconda3/lib/python311.zip' because it is not a valid directory
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)   Received 4 paths from interpreter
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)     /Users/jdd31/anaconda3/lib/python3.11
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)     /Users/jdd31/anaconda3/lib/python3.11/lib-dynload
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)     /Users/jdd31/anaconda3/lib/python3.11/site-packages
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)     /Users/jdd31/anaconda3/lib/python3.11/site-packages/aeosa
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)   Looking in python search path '/Users/jdd31/anaconda3/lib/python3.11'
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/anaconda3/lib/python3.11'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/anaconda3/lib/python3.11'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Looking in python search path '/Users/jdd31/anaconda3/lib/python3.11/lib-dynload'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/anaconda3/lib/python3.11/lib-dynload'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/anaconda3/lib/python3.11/lib-dynload'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Looking in python search path '/Users/jdd31/anaconda3/lib/python3.11/site-packages'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/anaconda3/lib/python3.11/site-packages'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/anaconda3/lib/python3.11/site-packages'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Looking in python search path '/Users/jdd31/anaconda3/lib/python3.11/site-packages/aeosa'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/anaconda3/lib/python3.11/site-packages/aeosa'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/anaconda3/lib/python3.11/site-packages/aeosa'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Looking in bundled stubs path '/Users/jdd31/.vscode/extensions/ms-python.vscode-pylance-2023.12.1/dist/bundled/stubs'
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/.vscode/extensions/ms-python.vscode-pylance-2023.12.1/dist/bundled/stubs'
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/.vscode/extensions/ms-python.vscode-pylance-2023.12.1/dist/bundled/stubs'
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Looking for typeshed stdlib path
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Looking for typeshed stdlib path
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Typeshed path not found
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Looking for typeshed third-party path
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Looking for typeshed stubs path
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Typeshed path not found

debonte avatar Feb 07 '24 00:02 debonte

This is weird. Any chance you have typeshed installed in your conda environment? Asking because I searched through our old issues for Could not import 'builtins' and the only hit was an issue caused by installing typeshed. #4508 (comment)

2024-02-06 18:12:31.637 [info] [Info  - 6:12:31 PM] (31127) Could not import 'builtins' in file '/Users/jdd31/Desktop/Python Code/CS103/prob1.py'
2024-02-06 18:12:31.639 [info] [Info  - 6:12:31 PM] (31127)   Looking in stubPath '/Users/jdd31/Desktop/Python Code/typings'
2024-02-06 18:12:31.641 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/Desktop/Python Code/typings'
2024-02-06 18:12:31.641 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/Desktop/Python Code/typings'
2024-02-06 18:12:31.642 [info] [Info  - 6:12:31 PM] (31127)   Looking in root directory of execution environment '/Users/jdd31/Desktop/Python Code'
2024-02-06 18:12:31.642 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/Desktop/Python Code'
2024-02-06 18:12:31.643 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/Desktop/Python Code'
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)   Finding python search paths
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)   Executing interpreter: '/Users/jdd31/anaconda3/bin/python'
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)   Skipping '/Users/jdd31/anaconda3/lib/python311.zip' because it is not a valid directory
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)   Received 4 paths from interpreter
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)     /Users/jdd31/anaconda3/lib/python3.11
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)     /Users/jdd31/anaconda3/lib/python3.11/lib-dynload
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)     /Users/jdd31/anaconda3/lib/python3.11/site-packages
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)     /Users/jdd31/anaconda3/lib/python3.11/site-packages/aeosa
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)   Looking in python search path '/Users/jdd31/anaconda3/lib/python3.11'
2024-02-06 18:12:31.644 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/anaconda3/lib/python3.11'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/anaconda3/lib/python3.11'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Looking in python search path '/Users/jdd31/anaconda3/lib/python3.11/lib-dynload'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/anaconda3/lib/python3.11/lib-dynload'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/anaconda3/lib/python3.11/lib-dynload'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Looking in python search path '/Users/jdd31/anaconda3/lib/python3.11/site-packages'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/anaconda3/lib/python3.11/site-packages'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/anaconda3/lib/python3.11/site-packages'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Looking in python search path '/Users/jdd31/anaconda3/lib/python3.11/site-packages/aeosa'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/anaconda3/lib/python3.11/site-packages/aeosa'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/anaconda3/lib/python3.11/site-packages/aeosa'
2024-02-06 18:12:31.645 [info] [Info  - 6:12:31 PM] (31127)   Looking in bundled stubs path '/Users/jdd31/.vscode/extensions/ms-python.vscode-pylance-2023.12.1/dist/bundled/stubs'
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve stub package using root path '/Users/jdd31/.vscode/extensions/ms-python.vscode-pylance-2023.12.1/dist/bundled/stubs'
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Attempting to resolve using root path '/Users/jdd31/.vscode/extensions/ms-python.vscode-pylance-2023.12.1/dist/bundled/stubs'
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Looking for typeshed stdlib path
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Looking for typeshed stdlib path
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Typeshed path not found
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Looking for typeshed third-party path
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Looking for typeshed stubs path
2024-02-06 18:12:31.646 [info] [Info  - 6:12:31 PM] (31127)   Typeshed path not found

no, i do not have typeshed installed.

ghost avatar Feb 07 '24 00:02 ghost

Edit 2/6/2024 7:31pm EST : I managed to fix the issue. I completely uninstalled VSCode and all associated files and settings. I redownloaded it and reinstalled my plugins and it works fine. I still have no idea what caused this issue, but if it happens to anyone else my only advice is to completely delete and reinstall vscode, deleting the following files as well before you reinstall. ~/.config/Code ~/Library/Application Support/Code ~/Library/Caches/com.microsoft.VSCode ~/Library/Caches/com.microsoft.VSCode.ShipIt ~/Library/Preferences/com.microsoft.VSCode.helper.plist ~/Library/Preferences/com.microsoft.VSCode.plist ~/Library/Saved Application State/com.microsoft.VSCode.savedState

ghost avatar Feb 07 '24 01:02 ghost

I'm keeping this open in hopes i can eventually repro

ghost avatar Feb 07 '24 01:02 ghost

Please reactivate if you get a repro.

debonte avatar Feb 07 '24 01:02 debonte

Hi Team, This happens because of a issue in the extension Pylance. All you have to do is uninstall it and restart VsCode. This should fix the issue. If you need pylance for your work, try re-installing it or try for an older version. Let me know if this helps anyone, Kinds regards

eliguinza avatar May 22 '24 14:05 eliguinza

This seems to be reproducible by configuring something like

{
   "python.defaultInterpreterPath": "C:\\Tools\\Python2\\python.exe"
}

in VSCodes settings.json. This is done to use old-school Python2 as default runtime. A workaround can be applied by doing something like this: https://stackoverflow.com/a/77793780/1873366

dfs- avatar May 23 '24 12:05 dfs-

thank you for the repro. let us take a look whether we can guard from it.

heejaechang avatar May 23 '24 17:05 heejaechang

I encountered this issue working on a project in wsl2.

The extension page presented with: This extension is disabled in this workspace because it is defined to run in the Remote Extension Host. Please install the extension in 'WSL: jammy' to enable..

After clicking to install in WSL and restarting VScode the Pylance lint errors for built-ins like print(...) were resolved.

Just sharing in case it helps to troubleshoot...

pztrick avatar Jul 09 '24 11:07 pztrick

I can't repro the original issue anymore. closing it.

heejaechang avatar Jul 23 '24 18:07 heejaechang