Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot have a package named 'models' in project #110

Closed
RafaelWO opened this issue Nov 3, 2021 · 5 comments · Fixed by #151
Closed

Cannot have a package named 'models' in project #110

RafaelWO opened this issue Nov 3, 2021 · 5 comments · Fixed by #151

Comments

@RafaelWO
Copy link

RafaelWO commented Nov 3, 2021

I would like to have a directory/package called models within my project that uses this library. Sadly, this does not work.

Check out this minimal example:

.
├── main.py
└── models
    ├── bar.py
    └── __init__.py
# models/bar.py
print("I'm bar!")
# main.py
from models import bar

If I run main.py it gives the expected output:

I'm bar!

Now, I will import something from fhirclient in main.py:

# main.py
from fhirclient import client

from models import bar

If I now run main.py I get the following error:

Traceback (most recent call last):
  File "/path/to/playground/main.py", line 2, in <module>
    from models import bar
ImportError: cannot import name 'bar' from 'models' (/path/to/fhirclient-package/fhirclient/fhirclient/models/__init__.py)

I guess this is due to adding the directory models in from fhirclient to sys.path in the parent __init__.py file:

# fhirclient/__init__.py
import sys
import os.path
abspath = os.path.abspath(os.path.dirname(__file__))
if abspath not in sys.path:
    sys.path.insert(0, abspath)
@mzbik
Copy link
Contributor

mzbik commented Aug 26, 2022

This also happens with directory named "server". It seems pretty odd practice for a module init to futz with the system path...

All the imports within fhirclient need to be self-relative except that fhir-parser has non-relative imports and so we'd have to fix that. Sigh. I think we're at the point of trial-and-error avoiding directory names.

@ducu
Copy link
Collaborator

ducu commented Aug 27, 2022

Guys if you come up with a fix for this it would be great, looking fwd for pull requests.

@p2-apple
Copy link

I think the original hack with sys path was to support Python 2 and 3. You could consider finally dropping Python 2 and revamp all the imports.

I remember those being a headache, see e.g. 7ee688a

@ducu
Copy link
Collaborator

ducu commented Aug 29, 2022

Yes dropping Python 2 could be the way to go, feel free to take this on

@mzbik
Copy link
Contributor

mzbik commented Nov 4, 2022

See PR #134

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants