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

Python: import streamsx.rest_primitives raises ImportError #2476

Closed
ghost opened this issue Jun 10, 2020 · 2 comments · Fixed by #2481
Closed

Python: import streamsx.rest_primitives raises ImportError #2476

ghost opened this issue Jun 10, 2020 · 2 comments · Fixed by #2481
Labels

Comments

@ghost
Copy link

ghost commented Jun 10, 2020

>>> from streamsx.rest_primitives import Instance
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rolef/infosphereStreams/development/tk/streamsx.topology/com.ibm.streamsx.topology/opt/python/packages/streamsx/rest_primitives.py", line 40, in <module>
    import streamsx.topology.context
  File "/home/rolef/infosphereStreams/development/tk/streamsx.topology/com.ibm.streamsx.topology/opt/python/packages/streamsx/topology/context.py", line 27, in <module>
    import streamsx.rest
  File "/home/rolef/infosphereStreams/development/tk/streamsx.topology/com.ibm.streamsx.topology/opt/python/packages/streamsx/rest.py", line 71, in <module>
    from .rest_primitives import (Domain, Instance, Installation, RestResource, Toolkit, _StreamsRestClient, StreamingAnalyticsService, _streams_delegator,
ImportError: cannot import name 'Domain'

Importing the whole module import streamsx.rest_primitives also raises ImportError.

Current workaround: Do import streamsx.topology.context before importing anything from rest_primitives.

@ghost ghost added the bug label Jun 10, 2020
@ghost ghost self-assigned this Jun 10, 2020
@ghost
Copy link
Author

ghost commented Jun 10, 2020

There are lots of circular imports, which cause these problem. A good discussion about circular imports and its problems is on stackoverflow.com:
https://stackoverflow.com/questions/22187279/python-circular-importing

@ghost
Copy link
Author

ghost commented Jun 11, 2020

There are multiple dependencies between the modules streamsx.topology.context, streamsx.rest, streamsx.rest_primitives, and streamsx.build. When a module is imported, all code in it is executed. The role of __init__.py is not considered here.

When importing streamsx.rest_primitives, following happens:

  1. streamsx.topology.context is imported
  2. context imports streamsx.rest
  3. streamsx.rest imports various identifiers from streamsx.rest_primitives. (from streamsx.rest_primitives import ...)
  4. These identifiers are not yet defined as the importer is still at the beginning importing this module --> ImportError

With circular dependencies no from xyz import nnn should be done. The better choice would be to avoid circular imports by a clean refactoring.

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

Successfully merging a pull request may close this issue.

0 participants