I'm working on a Python project where I've to communicate with the Jira API. I want to use the "offical" Jira client for Python which is available here.
The package introduces a global namespace jira
, meaning you have to
from jira import JIRA
to use the API.
How can I create a sub-package
company.project.datasources.jira
without shadowing the global jira
package?
Let me add a few steps to reproduce my problem and to show what I have in mind:
This is my directory layout:
.
├── company
│ ├── __init__.py
│ └── project
│ ├── datasources
│ │ ├── __init__.py
│ │ └── jira.py
│ └── __init__.py
└── test.py
company/project/datasources/jira.py
# This should be the global class. I don't know how to import it
from jira import JIRA as BaseJira
class JIRA(BaseJira):
# Add something fancy here
pass
test.py
from company.project.datasources.jira import JIRA
custom_jira = JIRA()
When I try:
python test.py
I get the following error:
Traceback (most recent call last):
File "test.py", line 1, in <module>
from company.project.datasources.jira import JIRA
File "/tmp/test/company/project/datasources/jira.py", line 2, in <module>
from jira import JIRA as BaseJira
ImportError: cannot import name JIRA
The __init__.py
are currently all empty files. I've tried a lot with them but it doesn't worked for me. Probably these files are they key to my problem?
You're using Python 2, which has some issues with how it resolves imports. It thinks
jira
refers to thejira
module relative to the current module (in this case, they refer to the same thing, but this would also happen if you also tried to do this import from another module next tojira.py
).You need to enable Python 3's import behavior. Add the following import before anything else in
jira.py
(or anything else at the same level that would see the shadowed package):See PEP 328 for more information. The abstract describes the general problem: