0

I have two different Python projects with the same file structure. The first project is:

.algbot_indicators (Project name)
--> src
----> algbot
------> indicators
--------> datasetreader

I created a *whl file in the first project order to use in the second project. Then, I installed this *whl into the second project. The second project's file structure is as follows:

.algbot_ai (Project name)
--> src
----> algbot
------> ai

When I try to use a class from the first project, I get an error:

from src.algbot.indicators.datasetreader.DatasetReader import DatasetReader
"cannot find reference indicators ..." 

I understand that Python confuses the 2 projects as they start with the same name: src. When I change one of the projects root folder to "source", the import works. However, this naming convention is the one we always use.

So, how can we import the first project's class into the second without Python confusing? Is there any way or do we have to use different root package name?

iso_9001_
  • 2,655
  • 6
  • 31
  • 47
  • How did you create the `.whl` files? The `src` should not normally be part of the import namespace. In any case, you should correctly package your projects using the [packaging namespace package](https://packaging.python.org/guides/packaging-namespace-packages/) guide. – metatoaster Dec 08 '20 at 12:22
  • Each package should have its own unique top-level name. For example, `from algbot_indicators.src.x.y import z` and `from algbot_ai.src.x.y import z` – gmdev Dec 08 '20 at 13:13
  • @gmdev We solved the problem using your suggested method (changed the top-level name). So, it is not possible to use the same topl-level name, is it? – iso_9001_ Dec 09 '20 at 07:27
  • @metatoaster Using instructions [here](https://stackoverflow.com/questions/26059111/build-a-wheel-egg-and-all-dependencies-for-a-python-project) – iso_9001_ Dec 09 '20 at 07:28
  • @iso_9001_ That guide you link does not apply to the situation you are in (as it does not cover your specific use case, which is solved through usage of namespaces and creating a `setup.py` with the right declaration to address that, plus you may need to add the intermediate `__init__.py` as documented), please refer to the guide I have linked. – metatoaster Dec 09 '20 at 08:37
  • @gmdev your advice was misleading - it is completely possible to use the same top-level name as OP desired, please refer to the namespace package guide I've linked. – metatoaster Dec 09 '20 at 08:42
  • Ah, okay. I didn't know that was possible - thanks for the article! So if you have two packages and both top-level modules have the same name, all the OP needs to do is include `name='src'` in `setup.py` and `__import__('pkg_resources').declare_namespace(__name__)` in each `__init__.py`? – gmdev Dec 09 '20 at 12:50

0 Answers0