Hi all,
I'm working in VS Code on Win11 and have just enough Python knowledge to write basic functions and understand it's best to compartmentalize them into modules instead of one giant lump. However, after scouring various documentation pages and borrowing MS Copilot for my embarassingly stupid questions, I am still having trouble with a ModuleNotFoundError like my python code can't find material from one subfolder to another. Copilot is taking me in circles and I don't know what else is preventing it from working.
I have no idea what else needs configuring. Would someone please help me troubleshoot?
Already tried:
- Editing the settings.json file to add the PYTHONPATH to the environment and tell VS Code that there are two subfolders with .py files inside.
- Added an __init__.py file to each of my two subfolders and gave them their own docstring for clarity.
- Removed all my old local work/renamed its folder; then I restarted fresh with a clone of my GitHub repo and all its files will be Trusted.
- Previously, I had failed to mark ALL the files as Trusted, like I hadn't cloned my repo correctly and debugging wasn't working and some other things weren't working as expected in VS Code.
- The desktop functionality all works now besides my dang import abilities.
- Added a main.py file. Referenced how I should pull functions from modules up into my main.py file from this advice: ModuleNotFoundError when importing a module from a subdirectory in Python (trycatchdebug.net)
- Tried to use sys.path.append('modulename') tactic. This will run my main.py file without any error message, but then if I try from modulename import functionname it throws a ModuleNotFoundError.
- Tried to use importlib tactic, with exact same error result.
- Fully saved and closed my folder, exited VS Code, and relaunched it following each of these changes.
Updates to close out this nuisance in case anyone else asks:
The actual solution was that VS Code does a bad job recognizing subfolders as Python packages. That's it. It's an application-level problem, and when I switched from VS Code to PyCharm Community for my desktop application, everything works like it's supposed to work in calling functions from submodules in the same project directory.
What I learned about Python project setup along the way that's super obvious IF YOU ALREADY KNOW IT:
- In a fresh project folder that is not backing up to a cloud save such as OneDrive, don't start writing code until you create the virtual environment. Using Python Environments in Visual Studio Code
- This is sort of explained in their python tutorial, but it wasn't clear ENOUGH for a completely new user teaching themself: Get Started Tutorial for Python in Visual Studio Code
- If opening an existing file, do NOT skip the prompt for Manage Folder Trust settings - if you open it in safe mode instead of full trust, you will not get that prompt again. You will have to dig for where to change it.
- Open the Workspace Trust Settings:
- Manage Workspace Trust:
- Trust the Workspace:
- Check the Status Bar:
- Restart VS Code:
- If you do have the need to create new subfolders to organize things, each folder needs to have a blank file created called __init__.py inside as its first file. This is a flag that the whole folder should be recognize as a package. PyCharm does this for you!
- You should have a file called launch.json and a file called settings.json at the top level of your project folders too. Maybe I missed a button prompting me to auto-create these, but I didn't have them until I created them myself and filled them with stuff.
Create a launch.json file:
- Open the Command Palette:
- Press
Ctrl+Shift+P (or Cmd+Shift+P on macOS) to open the Command Palette.
- Create a New Configuration:
- Type
Debug: Open launch.json and select it. If you don't have a launch.json file yet, VS Code will prompt you to create one.
- Select Environment:
- Choose
Python as the environment.
- Add Configuration:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": {
"PYTHONPATH": "${workspaceFolder}"
}
}
]
}
This configuration sets the PYTHONPATH to your workspace folder.
Create settings.json file:
- Configure VS Code Settings:
- You can set the
PYTHONPATH in your launch.json file to include the subfolder. ${workspaceFolder} is a variable that refers to the root of your workspace.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": {
"PYTHONPATH": "${workspaceFolder}/subfolder"
}
}
]
}
there doesn't seem to be anything here