1

I have seen many responses suggesting including an __init__.py file in the subdirectory of submodules in order to import them as python package, but I can't get it working for my project. My project directory structure looks like this:

helm-2022
├── model
│   ├── __init__.py
│   ├── model.ipynb
│   └── torchModelSummary.py
├── preprocess
│   └── preprocess.py
└── utils
    ├── __init__.py
    └── vis_utils.py

I want to import functions inside vis_utils.py in the notebook model.ipynb under model folder and preprocess.py under the preprocess folder. I have already added empty __init__.py under the utils folder. When I tried to import in model.ipynb using from utils import vis_utils, I still got No module named 'utils'. I have also tried to import by including the top directory from helm-2020.utils import vis_utils, but that gives me a syntax error because of the hyphen. I don't have permission to change the top directory name, so changing the hyphen is not an option. Thank you so much in advance.

Michael Butscher
  • 10,028
  • 4
  • 24
  • 25
Shufan Xia
  • 29
  • 4
  • The problem is the module you want to import is not in the same directory. it is in a subdirectory of the parent directory. – Amir Sabzehparvar Jun 29 '22 at 21:33
  • You can find the answer here: https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder – Amir Sabzehparvar Jun 29 '22 at 21:34
  • You have a bit of a pickle. The only meaningful top-level package would be `helm-2022`, but importing a name with a dash isn't possible from an `import` statement (though you can use [`importlib`](https://stackoverflow.com/a/24659400/11082165)). If you really can't change the name of that directory, your best best would be to create a new directory inside of `helm-2022` (maybe `helm_2022`?) and make that directory the package root. – Brian61354270 Jun 29 '22 at 21:38
  • Also, `__init__.py` is only required to create packages in Python versions older than 3.3. See [PEP 420](https://peps.python.org/pep-0420/) for more details. – Brian61354270 Jun 29 '22 at 21:43
  • Its that top directory name that's the problem. If you wanted to do relative imports, that top directory would be the package name, but as you've noted, that hyphen messes everything up. You could make each of the `model`, `preprocess` and `utils` into their own packages (add helm-2022 to the PYTHONPATH), but those are horrible names for packages and really should be together. I don't know a good name for this whole bunch of code together, but if you could create a directory with an awesome name, and move those subdirectories down, then you have something to work with. – tdelaney Jun 29 '22 at 21:52
  • Generally, once you get to the level where you are creating packages, you should make them build and installable with a setup.py. That discipline gives you a much better structure. Python kinda lets you get away with not installing your code, but that breaks down pretty quickly. – tdelaney Jun 29 '22 at 21:54
  • 1
    @tdelaney thank you so much. First time using setup.py. Thanks to these two documents: [writing setup.py](https://docs.python.org/3/distutils/setupscript.html )and [installing](https://docs.python.org/3/install/ ). It works perfectly. I will keep using this approach. – Shufan Xia Jun 30 '22 at 01:12

0 Answers0