You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've encountered a persistent issue in Visual Studio Code (VSC) where I'm experiencing import errors when trying to import one Python file into another. The error is indicated by a yellow underline beneath the import statement, and the Problems tab states: "Import File could not be resolved Pylance(reportMissingImports)". The peculiar thing is, the same code runs perfectly fine in PyCharm, which suggests that this issue is specific to VSC or its extensions.
I have a simple project structure with two Python files in the same directory:
file1.py
import file2 file2.my_function()
file2.py
def my_function(): print("Hello from file2!")
Example Test File:
test_file.py
`import file2
def test_my_function():
assert file2.my_function() == "Hello from file2!"`
The issue appeared mid-coding session; previously working imports suddenly started showing the error.
The file paths are "C:\Users[username]\import_test\file1.py" and "C:\Users[username]\import_test\file2.py".
The issue persists even with a fresh installation of Python and VSC, and with all Python-related folders deleted.
The code runs without any import errors in PyCharm.
The import statement works to import a function or class, but it is labeled as a defect import, and pytest does not detect it unless it is testing right out of the file.
Troubleshooting Steps Taken:
Check File and Folder Names: Ensured that there are no spaces or special characters in file and folder names.
Python Interpreter: Ensured the correct Python interpreter is selected in VSC. Even tried using the Python interpreter from PyCharm in VSC.
Python Extension: Verified that the Python extension is installed and enabled.
Disable Other Extensions: Only the Python extension is enabled.
Absolute Import: Tried modifying sys.path to include the directory containing the Python files.
File Encoding: Checked and confirmed both Python files are set to UTF-8.
Reinstallation: Completely uninstalled and reinstalled both Python and VSC, ensuring all configuration files and settings were cleared.
Switched to Jedi: Changed the Python Language Server to Jedi in the settings.json file, and back after it became appearant it didnt cause the issue
Compared CWDs: Compared the current working directories in different environments using os.getcwd() to ensure they are the same.
Cleared Cache and Extension Data: Cleared cache and extension data.
Added init.py File: Added an init.py file to the directory, with no result.
Used Relative Import: Tried using relative import from .file2 import my_function, with no result.
Reinstalled pytest: Uninstalled and reinstalled pytest, with no result.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I've encountered a persistent issue in Visual Studio Code (VSC) where I'm experiencing import errors when trying to import one Python file into another. The error is indicated by a yellow underline beneath the import statement, and the Problems tab states: "Import File could not be resolved Pylance(reportMissingImports)". The peculiar thing is, the same code runs perfectly fine in PyCharm, which suggests that this issue is specific to VSC or its extensions.
I have a simple project structure with two Python files in the same directory:
file1.py
import file2 file2.my_function()
file2.py
def my_function(): print("Hello from file2!")
Example Test File:
test_file.py
`import file2
def test_my_function():
assert file2.my_function() == "Hello from file2!"`
The issue appeared mid-coding session; previously working imports suddenly started showing the error.
The file paths are "C:\Users[username]\import_test\file1.py" and "C:\Users[username]\import_test\file2.py".
The issue persists even with a fresh installation of Python and VSC, and with all Python-related folders deleted.
The code runs without any import errors in PyCharm.
The import statement works to import a function or class, but it is labeled as a defect import, and pytest does not detect it unless it is testing right out of the file.
Troubleshooting Steps Taken:
Beta Was this translation helpful? Give feedback.
All reactions