0

Its been a long time since I've used python but I don't recall it ever being this hard to import code.

I have a project structure like this

├── main.py
└── tutorial_quickstart
    ├── __init__.py
    ├── data.py
    ├── neural_net.py
    ├── test.py
    └── train.py

My main.py script imports code like so:

import torch
from torch import nn
from tutorial_quickstart.neural_net import NeuralNetwork
from tutorial_quickstart.data import get_training_data
from tutorial_quickstart.train import train
from tutorial_quickstart.test import test

The problem is with the main.py script, I want to move this main.py into tutorial_quickstart and run

$ python tutorial_quickstart/main.py

❯ python tutorial_quickstart/main.py
Traceback (most recent call last):
  File "/home/colin/code/chess-ml/tutorial_quickstart/main.py", line 3, in <module>
    from tutorial_quickstart.neural_net import NeuralNetwork
ModuleNotFoundError: No module named 'tutorial_quickstart'

However when ran from the project root directory via python main.py everything works fine

Some context:

I'm using WSL2, vscode and anaconda, I created a conda environment inside my project directory via vscode. Then manually select the python interpreter to be the one within my project directory.

Colin Cheung
  • 148
  • 2
  • 13
  • 2
    Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time). While this question isn't about relative imports, it's based in the same misconceptions about packages vs directories and modules vs scripts – Brian61354270 Jul 22 '23 at 22:12
  • Okay this explains it thanks! I also got some errors from pylint for when I changed my imports to without `tutorial_quickstart`. If it helps anyone here's a github issue that fixed it for me https://github.com/dense-analysis/ale/issues/208#issuecomment-265590465 – Colin Cheung Jul 23 '23 at 12:46

0 Answers0