1

To detail the issue, let's say I have the following couple of pyproject.toml for my package that is to be used in another project (described later on below), one with different name than the root directory name:

[tool.poetry]
name = "my-module-one"
version = "0.0.1"
description = ""
authors = []
packages = [
  { include = "some_module" }
]
[tool.poetry]
name = "my-module-two"
version = "0.0.1"
description = ""
authors = []
packages = [
  { include = "some_module" }
]

The project's directory structure is something like this (all managed using Poetry)

my-project/
|- my-module-one/
|  |- pyproject.toml
|  |- some_module/
|     |- something.py
|- my-module-two/
|  |- pyproject.toml
|  |- some_module/
|     |- something.py
|- app/
   |- main.py
   |- pyproject.toml

Now normally, we would install and import from either one using Poetry like so:

app$ poetry add ../my-module-one

And import it like so

# File: app/main.py
from some_module import something

This becomes a problem when importing both modules are installed because they export a directory with the same name in this case (and that I'm in no power to change this). So I would like to be able to import like so:

# Import some_module from my-module-one
from some_module_one import something as something_one

# Import some_module from my-module-two
from some_module_two import something as something_two

Is it possible to achieve this? If so, how can we do it?

P.S.: I kind of wanted to somehow alias the export from either one of the modules so my-module-one's something becomes something_one, but can't figure out a way.

ionizer
  • 1,661
  • 9
  • 22
  • `from some_module_one import something as something_one` This is how we do aliasing in python exactly as you mentioned. Is it giving some error? – Roy Jun 07 '21 at 09:11
  • The issue is that I have two modules that exports the package called `some_module` in this example, and I need to install them both (my-module-one and my-module-two) in the `app` project. This is a problem because there's package name conflict. What that does is aliasing the import. – ionizer Jun 07 '21 at 09:14
  • So both of the modules have the same name as well as the class/function you are interested in also has the same names? – Roy Jun 07 '21 at 09:18
  • Yes, the ones I'm concerned of even has the same function name in both modules. So, both `something.py` has a function with the name of some_function for example. – ionizer Jun 07 '21 at 09:21
  • Short answer seems `NO`. More details here. https://stackoverflow.com/questions/5062793/is-it-possible-to-use-two-python-packages-with-the-same-name#:~:text=Say%20you%20are%20given%20two,)%20mod3.py%20... – Roy Jun 07 '21 at 09:21
  • It might be possible if we can somehow alias the exports from the modules, but sadly I can't quite figure out a way to do this except changing the directory name and therefore directly changing the exported package name (which I don't think I'm in power to do this as this isn't my project). – ionizer Jun 07 '21 at 09:26

0 Answers0