I have a project that looks like this :
.
├── A
│ ├── setup.py
│ ├── __init__.py
│ ├── a.py
│ └── data.py
└── B
├── b.py
└── data.py
I don't have control over A
but I would like to use it within B/b.py
. I can't rewrite any code within folder A
(except setup.py
).
I tried this:
# File A/setup.py
from setuptools import setup, find_packages
setup(name='packageA', version='1.0', packages=find_packages())
With the following installation/tests:
$ cd B
$ pip install -e ../A
$ python -c "import a; print(a)"
<module 'a' from 'A/a.py'>
$ python -c "import data; print(data)"
<module 'data' from 'B/data.py'>
# How to get the same for <module 'data' from 'A/data.py'>?
The name collision bothers me here, I would like to be able to import both A/data.py
and B/data.py
in b.py
. I wanted to know if there is a way around this?
I would like to be able to write something like:
$ python -c "from A import data; print(data)"
<module 'data' from 'A/data.py'>
I tried to have setup.py
one level above (in the root directory), but if I do that then I have problems within A
:
$ cd B
$ pip install -e ..
$ python -c "from A import data; print(data)"
Traceback (most recent call last):
File "A/data.py", line 1, in <module>
from a import some_function
ImportError: cannot import name 'some_function' from 'a' (unknown location)
If I could rewrite A/data.py
I could just do this (but I can't):
from A.a import some_function
Is it possible to modify setup.py
to encapsulate A
under a (fake) module name? Any solution is welcome.