Paths behave differently on Git folders and workspace folders

Git folders can reference the project root, while workspace folders reference the current working directory.

Written by caio.cominato

Last published at: October 18th, 2024

Problem

When you run the same code on a Git folder (AWSAzureGCP) vs. a workspace folder (AWSAzureGCP), the code fails with a ModuleNotFoundError: No module named 'some_folder' error message.

This can cause confusion, especially if you are debugging code from a Git project outside of the Git project.

Cause

The error occurs because imports for Git folders can reference the project’s root. When using workspace folders, the root references the current working directory.

Example

| some_git_folder (GIT)
| -- some_module
| ---- some_component.py
| -- some_other_folder
| ---- notebook.ipynb
| -- some_file.json
...
| some_standard_folder
| -- some_module
| ---- some_component.py
| -- some_other_folder
| ---- notebook.ipynb
| -- some_file.json

In this example, we have a module present under some_folder, a notebook under some_other_folder and a file present on the root.

We mirrored the content across the folders.

The file contents are as follows:

some_component.py:

%python

from dataclasses import dataclass

@dataclass
class MyModuleClass:
    my_string: str
    my_int: int

    def __repr__(self):
        return f"{self.my_string} - {self.my_int}"

notebook.ipynb:

%python

# cell [1]
from some_module.some_component import MyModuleClass

data = MyModuleClass("John", 52)
print(data)

# cell [2]
import json

with open('some_file.json') as f:
    data_json = json.loads(f.read())

print(data_json)

In this example the notebook outputs under the Git folder.

Output for notebook.ipynb for a Git folder.

cell [1] Output
John - 52

cell [2] Output
{'some_key': 'some_value'}

When the code is run under the workspace folder, the output fails for the first cell and succeed for the second cell.

Output for notebook.ipynb for a standard folder.

cell [1] Output
ModuleNotFoundError: No module named 'some_folder'

cell [2] Output
{'some_key': 'some_value'}

This happens because the root folder is different across both locations, even though the notebook code is the same.

Solution

You can resolve this issue in two ways.

  1. Move the underlying module files so the necessary files are referenced in the same directory as the code is located.
  2. Move your notebook to the root directory and refactor your code accordingly.