pythonvisual-studio-codecodespacesgithub-codespaces

ModuleNotFoundError while using GitHub codespace editor


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.


Solution

  • 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:

    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: