1

I can't manage to import some functions in the files. Here is the structure

.
├── main.py
├── src
│   ├── mypandas.py
│   ├── labelling.py

Labelling uses class from mypandas and mypandas uses functions from labelling.

Labelling imports mypandas as

import mypandas as myp

mypandas imports labelling as

import labelling as l

In main, I import both as

import src.labelling as sl
import src.mypandas as sp

Which gives me an error,: module not

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 7
      5 import src.webscraping as sw
      6 import src.viz as sv
----> 7 import src.mypandas as sp
      9 import sqlite3
     10 import pandas as pd 

File ~/Desktop/code/Instagram_bot_classification/src/mypandas.py:5
      1 def foo (): 
      2     print('oui')
----> 5 import labelling
      6 import pandas as pd
      7 import plotly.express as px

ModuleNotFoundError: No module named 'labelling'

How can I change the folder structure or code to not get an error but still to what I want?

Marc
  • 522
  • 1
  • 5
  • 15
  • Try import .labelling in mypandas.py – Alpensin Jan 15 '23 at 19:52
  • For starters, "*Labelling uses class from mypandas and mypandas uses functions from labelling*" is not a good design, as it will immediately lead to circular imports (which is not what your error is, BTW). – MattDMo Jan 15 '23 at 19:52
  • Usually you don't import `src` explicitly. – Maurice Meyer Jan 15 '23 at 19:53
  • 1
    @Marc: Did you look at [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Maurice Meyer Jan 15 '23 at 19:55
  • @MattDMo, how should I design it instead? merge both files and just import from one? – Marc Jan 15 '23 at 20:09
  • 1
    That's probably the easiest way to go. Make a `utils.py` and put common, shared stuff in there. If there's still significant functionality to be coded, keep `mypandas.py` and/or `labelling.py`, and have both `import .utils`. If it makes sense to put *everything* together, just `import src.utils` in `main.py`, then adjust your other references to `sl` and `sp` accordingly. – MattDMo Jan 15 '23 at 20:14

0 Answers0