ModuleNotFoundError while using GitHub codespace editor

Question:

Recently I started using GitHub codespace for the first time. I created a new codespace from one of my repositories. Assuming the folder structure as below:

my-codespace
|--- utils
|------ my_script.py
|--- config.py

I get the following error when I import config.py inside my_script.py:

ModuleNotFoundError: No module named 'config'

Autocomplete is working inside the editor, and it recognizes config.py while I try to import it

enter image description here

After a while red line appears indicating an error

enter image description here

All methods, classes, and variables belonging to config.py are available inside my_script.py by autocomplete.

Currently, I’m using PyCharm on my computer and have no issues with that repository but I get the same error when I run it on local VS Code.

I would appreciate it if someone could help me in this regard.

Asked By: Babak

||

Answers:

I create a test file with the following structure and open the my-codespace folder in vscode.

my-codespace
├─ config.py
└─ utils
   └─ my_script.py

enter image description here

To fix the above error, add the following code at the top of the my_script.py file.

import sys
sys.path.append("./")

enter image description here

Answered By: JialeDu

After a few days of research and inquiry, I was able to solve most of the problems. I will update this post whenever I find out the answer to the remaining part.

As I said in the original post, this issue exists in both local and GitHub versions of VS Code. Someone suggested setting PYTHONPATH to the path of my custom module (config.py here). This might work for you, but it didn’t work for me. If you prefer to do that, you can run the following to make sure the path is set correctly:

import os
print(os.environ)

For the local version, I solved the problem by doing these:

  • Adding the following lines to the usersettings.json file made Python script files in subfolders find my custom module in the root folder:

    "terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}"
    }
    

    Note that I’m running my code on a Windows machine. If you are using Linux-based machines, you should use "terminal.integrated.env.linux" instead.

  • I also created a .env (no filename, just an extension) file inside the root folder, and then I added PYTHONPATH=Y:Programmingmy-codespace to it. By doing this, Jupyter notebooks are also able to find custom modules inside subfolders. If you prefer to use any other file name say vscode.env you can change it inside the application settings.

Obviously, you will need to reload your VS Code window after applying these changes. Just press CTRL+SHIFT+P and type reload window.

For the GitHub codespace version, I tried to do the same things as above:

  • I added the Linux version (terminal.integrated.env.linux) to the settings.json file to make Python script files in subfolders work without any issues.
  • Adding PYTHONPATH=${PYTHONPATH}:${workspaceFolder} (${workspaceFolder} is the alias for the project’s root folder) to the .env file didn’t help to run Jupyter notebooks inside the subfolders and I got an importing error as before. This is the remaining unresolved part of this issue.
Answered By: Babak