-1

I know this question has been asked a ton, but I can't figure out where I'm messing up. I am trying to import a module, FoodDelivLibrary.Utils but when I run it on the raspberry pi it says "module does not exists" It is weird because it works fine in pycharm but runs into issurs when I use terminal.

This is my file structure

The DriveTrain.py file is trying to access functions from the Serial.py file and I keep getting the error "No module named 'FoodDelivLibrary'" Any help would be much appreciated. Thank you!

I Have a init.py in each folder.

The import it is having trouble running is:

from FoodDelivLibrary.Utils.Serial import Serial

Here is the github if you want a clearer view of the file structure: https://github.com/archishou/FoodDeliv

  • What file is trying to import which module? from inside `DriveTrain.py`, you're trying to import `FoodDelivLibrary`? You're including too much extra information that clutters the question and makes it hard to help you. – Sean Pianka Jul 26 '18 at 21:38
  • Drivetrain.py is trying to import Serial.py, so DriveTrain.py is trying to import the FoodDelivLibrary module, the import it is trying to do is ` from FoodDelivLibrary.Utils.Serial import Serial` – Archishmaan P Jul 26 '18 at 21:40
  • Possible duplicate of [ImportError: No module named ](https://stackoverflow.com/questions/43476403/importerror-no-module-named-something) – GetHacked Jul 27 '18 at 14:34

2 Answers2

0

You are missing __init__.py's within your folders (to indicate that they are import-able packages).

app
├── __init__.py
├── a
│   └── __init__.py
└── b
    ├── c
    │   ├── __init__.py
    │   └── drive_train.py
    └── d
        ├── __init__.py
        └── serial.py

Within app/__init__.py contains from app.b.d import serial.

Within app/b/d/serial.py contains from app.b.c import drive_train.

Within app/b/c/drive_train.py contains print("imported!").

Importing app from app/.. prints imported!

I'll let you connect this example with your own code.

Sean Pianka
  • 2,157
  • 2
  • 27
  • 43
0

Try adding this to the top of your DriveTrain.py file:

import sys, os
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from Utils.Serial import Serial

It adds the path above the current file.

PixelEinstein
  • 1,713
  • 1
  • 8
  • 17
  • Fixing the directory structure to include `__init__.py` in places where there should be is a better solution than modifying the path. – Sean Pianka Jul 26 '18 at 21:59
  • He has `__init__.py` in each folder he stated in the question, but did not include the proper imports in the files. With smaller projects it doesn't really matter too much, with large applications I would recommend building the correct `import` in each init. – PixelEinstein Jul 26 '18 at 22:08
  • No, he does not have an `__init__.py` in each folder stated in the question. Both `Motors` and `Utils` _also need_ `__init__py`. – Sean Pianka Jul 26 '18 at 22:08
  • "I Have a init.py in each folder." – PixelEinstein Jul 26 '18 at 22:09
  • And shown by the directory structure, he clearly does not. The solution is not to modify the path, the solution is to add `__init__.py`'s and learn to correct import modules within a hierarchy. – Sean Pianka Jul 26 '18 at 22:09
  • I believe he added that statement so he would not have to include each __init__ in his structure above. Like I said, if he just needs that one import, modifying the path is not a ***wrong*** way to do it, it completes what he wants. With more complicated structures, I would recommend utilizing the init files. – PixelEinstein Jul 26 '18 at 22:12
  • I tried adding init.py to every folder and had no luck – Archishmaan P Jul 27 '18 at 11:32