0

I have a directory of scripts and folders. I have made separate scripts for organization purposes. The directory consists of PARENTFOLDER\modules\inherited\classes.py I have an init.py file in both modules and inherited because of dependencies in other scripts. I have another script called sequences.py in modules that has from inherited import classes. It's imported separately from a script called main.py in PARENTFOLDER. The problem is, when I run the script from main.py, it throws the error ModuleNotFoundError: No module named 'inherited'. It doesn't highlight 'inherited' in the syntax either. I don't know why it isn't recognising inherited as a module.

Currently, this is what the directory looks like:

.
├── assets
├── modules
│   ├── __pycache__
│   ├── inherited
│   │   ├── __init__.py
│   │   └── classes.py
│   ├── __init__.py
│   └── sequences.py
└── main.py
InSync
  • 4,851
  • 4
  • 8
  • 30
Gooberton
  • 17
  • 7
  • Could you provide a structure tree of your directory? Currently it's hard to tell which is in which. – InSync Aug 21 '23 at 15:03
  • 1
    @InSync I have added a screenshot of the directory. Tell me if this helps. – Gooberton Aug 21 '23 at 15:07
  • Inherited doesn't exist. modules.inherited does – Achille G Aug 21 '23 at 15:10
  • The script that is importing classes.py is inside modules. So `modules.inherited` doesn't exist to the script. I have tried and it just says 'modules does not exist'. – Gooberton Aug 21 '23 at 15:12
  • 2
    Thanks. I converted it to text, since [we prefer text to images](https://meta.stackoverflow.com/q/285551). See also questions on how to print a directory tree: [Linux](https://stackoverflow.com/q/3455625), [Windows](https://stackoverflow.com/q/9518646). – InSync Aug 21 '23 at 15:19

1 Answers1

1

Your import syntax seems to be incorrect, try using:

import modules.inherited.classes

or

from modules.inherited import classes
Comte_Zero
  • 252
  • 3
  • 19
  • It still says 'No module named inherited'. It appears to not be recognizing inherited as a class. – Gooberton Aug 21 '23 at 15:10
  • I have edited my code to be `import modules.inherited.classes as classes` but it still says that modules doesn't exist, which makes sense because sequences.py is *inside* modules. It doesn't recognize inherited as a module either. – Gooberton Aug 21 '23 at 15:19
  • It is an absolute import, it starts from the root of the project. which command do you use to run the script ? – Comte_Zero Aug 21 '23 at 15:21
  • The script is ran using main.py with `from modules import sequences`. The problem is coming from sequences.py in the line that attempts to import the classes.py module. – Gooberton Aug 21 '23 at 15:24
  • Yes but what is the command you run in your terminal ? – Comte_Zero Aug 21 '23 at 15:29
  • I don't run a command in the terminal. I run it from VSCode. The second option appears to be the correct answer. I have accepted your answer but I don't think it's registered yet. Thanks for your help! – Gooberton Aug 21 '23 at 15:31