Azure / azure-uamqp-python

AMQP 1.0 client library for Python
MIT License
57 stars 48 forks source link

ImportError: cannot import name 'Session' from partially initialized module 'uamqp' (most likely due to a circular import) #198

Closed rapsli closed 3 years ago

rapsli commented 3 years ago

Describe the bug Running this very simple example: https://github.com/Azure/azure-uamqp-python/blob/master/samples/sample_uamqp_receive_simple.py

To Reproduce Steps to reproduce the behavior:

  1. just run the sample and the following error shows up:
Traceback (most recent call last):
  File "mfr_app/queue.py", line 4, in <module>
    import uamqp
  File "C:\Users\schae\anaconda3\envs\test\lib\site-packages\uamqp\__init__.py", line 15, in <module>
    from uamqp.address import Source, Target
  File "C:\Users\schae\anaconda3\envs\test\lib\site-packages\uamqp\address.py", line 10, in <module>
    from uamqp import c_uamqp, compat, constants, utils
  File "C:\Users\schae\anaconda3\envs\test\lib\site-packages\uamqp\compat.py", line 18, in <module>
    import queue
  File "C:\Users\schae\OneDrive\Dokumente\dev\django_projects\urbenis\mfr_app\queue.py", line 5, in <module>
    from uamqp import authentication
  File "C:\Users\schae\anaconda3\envs\test\lib\site-packages\uamqp\authentication\__init__.py", line 8, in <module>
    from .cbs_auth import TokenRetryPolicy, CBSAuthMixin, SASTokenAuth, JWTTokenAuth
  File "C:\Users\schae\anaconda3\envs\test\lib\site-packages\uamqp\authentication\cbs_auth.py", line 13, in <module>
    from uamqp import Session, c_uamqp, compat, constants, errors, utils
ImportError: cannot import name 'Session' from partially initialized module 'uamqp' (most likely due to a circular import) (C:\Users\schae\anaconda3\envs\test\lib\site-packages\uamqp\__init__.py)

Expected behavior The sample should run

Additional context The issue is with this here:

from uamqp import authentication

yunhaoling commented 3 years ago

hey @rapsli, sorry for keeping you waiting.

I can't reproduce the issue locally, how did you install the uamqp library, is it via pip install? also do you include any dev path in your python environment like you clone the repo and include the project path in the python interpreter search path?

rapsli commented 3 years ago

yes. I tried again on a fresh conda environment, using python 3.9

installing via pip install

(urbenis) λ pip install uamqp
Collecting uamqp
  Downloading uamqp-1.2.13-cp39-cp39-win_amd64.whl (865 kB)
     |████████████████████████████████| 865 kB 6.8 MB/s
Requirement already satisfied: certifi>=2017.4.17 in c:\users\schae\anaconda3\envs\urbenis\lib\site-packages (from uamqp) (2020.12.5)
Collecting six~=1.0
  Using cached six-1.15.0-py2.py3-none-any.whl (10 kB)
Installing collected packages: six, uamqp
Successfully installed six-1.15.0 uamqp-1.2.13

I tried an even more striped down version. Opening python console and runnging import uamqp, but with the same result:

(urbenis) λ python
Python 3.9.1 (default, Dec 11 2020, 09:29:25) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import uamqp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\schae\anaconda3\envs\urbenis\lib\site-packages\uamqp\__init__.py", line 15, in <module>
    from uamqp.address import Source, Target
  File "C:\Users\schae\anaconda3\envs\urbenis\lib\site-packages\uamqp\address.py", line 10, in <module>
    from uamqp import c_uamqp, compat, constants, utils
  File "C:\Users\schae\anaconda3\envs\urbenis\lib\site-packages\uamqp\compat.py", line 18, in <module>
    import queue
  File "C:\Users\schae\OneDrive\Dokumente\dev\django_projects\urbenis\mfr_app\queue.py", line 5, in <module>
    from uamqp import authentication
  File "C:\Users\schae\anaconda3\envs\urbenis\lib\site-packages\uamqp\authentication\__init__.py", line 8, in <module>
    from .cbs_auth import TokenRetryPolicy, CBSAuthMixin, SASTokenAuth, JWTTokenAuth
  File "C:\Users\schae\anaconda3\envs\urbenis\lib\site-packages\uamqp\authentication\cbs_auth.py", line 13, in <module>
    from uamqp import Session, c_uamqp, compat, constants, errors, utils
ImportError: cannot import name 'Session' from partially initialized module 'uamqp' (most likely due to a circular import) (C:\Users\schae\anaconda3\envs\urbenis\lib\site-packages\uamqp\__init__.py)
>>>
yunhaoling commented 3 years ago

I also tried a new conda virtual env39, and install the uamqp via pip, nothing went wrong: image

could you print out the sys.path, I want to check if the sys.path has included some directories that cause problem?

rapsli commented 3 years ago

Here is print(sys.path):

['', 'C:\\Users\\schae\\anaconda3\\envs\\urbenis\\python39.zip', 'C:\\Users\\schae\\anaconda3\\envs\\urbenis\\DLLs', 'C:\\Users\\schae\\anaconda3\\envs\\urbenis\\lib', 'C:\\Users\\schae\\anaconda3\\envs\\urbenis', 'C:\\Users\\schae\\anaconda3\\envs\\urbenis\\lib\\site-packages']

rapsli commented 3 years ago

any news on this?

yunhaoling commented 3 years ago

sorry I am still not able to reproduce the issue locally.

but the logging you provide has this line which should not show up if we're purely importing just the uamqp.

import queue
  File "C:\Users\schae\OneDrive\Dokumente\dev\django_projects\urbenis\mfr_app\queue.py", line 5, in <module>

do you know where this ...\mfr_app\queue.py is coming from -- can we try to remove this file/module first? I'm wondering if the issue is caused by any module naming conflict like session or something else.

rapsli commented 3 years ago

Well, I wasn't maybe no precise enough. Here is the code I run:

import uamqp
from uamqp import authentication

Line 5 is with importing authentication

yunhaoling commented 3 years ago

@rapsli , those two lines you run them in the python console, right?

import uamqp
from uamqp import authentication

I tried an even more striped down version. Opening python console and runnging import uamqp, but with the same result:

I'm wondering if we're only running in the python console, then why C:\Users\schae\OneDrive\Dokumente\dev\django_projects\urbenis\mfr_app\queue.py would show up in the error traceback..

  File "C:\Users\schae\OneDrive\Dokumente\dev\django_projects\urbenis\mfr_app\queue.py", line 5, in <module>

also do you have other machines for test?

rapsli commented 3 years ago

Ok. I figured out more. it has to to with where in the path I am.

this works:

**C:\Users\schae\OneDrive\Dokumente\dev**
λ conda activate test

**C:\Users\schae\OneDrive\Dokumente\dev**
(test) λ python
Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import uamqp
>>> exit()

This doesn't work

**C:\Users\schae\OneDrive\Dokumente\dev**
λ conda activate test

**C:\Users\schae\OneDrive\Dokumente\dev**
(test) λ cd django_projects\urbenis\mfr_app\

**C:\Users\schae\OneDrive\Dokumente\dev\django_projects\urbenis\mfr_app** (master -> origin)
(test) λ python
Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import uamqp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\schae\anaconda3\envs\test\lib\site-packages\uamqp\__init__.py", line 15, in <module>
    from uamqp.address import Source, Target
  File "C:\Users\schae\anaconda3\envs\test\lib\site-packages\uamqp\address.py", line 10, in <module>
    from uamqp import c_uamqp, compat, constants, utils
  File "C:\Users\schae\anaconda3\envs\test\lib\site-packages\uamqp\compat.py", line 18, in <module>
    import queue
  File "C:\Users\schae\OneDrive\Dokumente\dev\django_projects\urbenis\mfr_app\queue.py", line 2, in <module>
    from uamqp import authentication
  File "C:\Users\schae\anaconda3\envs\test\lib\site-packages\uamqp\authentication\__init__.py", line 8, in <module>
    from .cbs_auth import TokenRetryPolicy, CBSAuthMixin, SASTokenAuth, JWTTokenAuth
  File "C:\Users\schae\anaconda3\envs\test\lib\site-packages\uamqp\authentication\cbs_auth.py", line 13, in <module>
    from uamqp import Session, c_uamqp, compat, constants, errors, utils
ImportError: cannot import name 'Session' from partially initialized module 'uamqp' (most likely due to a circular import) (C:\Users\schae\anaconda3\envs\test\lib\site-packages\uamqp\__init__.py)
>>>

It seems that as soons as I go some folders deeper into my workspace the import stops working.

But this works:

C:\Users\schae\OneDrive\Dokumente\dev\_test\_deeper\__deeper\___deeper
(test) λ python
Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import uamqp
>>>

There must be something in my project django folder which messes things up.

rapsli commented 3 years ago

:-D guess what. I figured out what the issue was!!

I have a file called queue.py in my folder that's the issue that will break everything. Renaming that file to something else will fix it. Maybe something for some notes somewhere...