WhyNotHugo / python-barcode

㊙️ Create standard barcodes with Python. No external dependencies. 100% Organic Python.
http://python-barcode.rtfd.io/
MIT License
572 stars 123 forks source link

Import Errors #161

Closed Cielquan closed 2 years ago

Cielquan commented 2 years ago

I try to use this library, but strangely the import won't work and I am clueless. Other third-party libs from PyPI are working flawlessly.

OS: Win 10 Python Versions: 3.10.5 | 3.8.10 Shell: (Git-)Bash

Reproduction

Create venv, activate it, update pip, install python-barcode. Then follow examples below

$ py -3.8 -m venv .venv && . .venv/Scripts/activate && python -m pip install -U pip && pip install python-barcode

Example 1

Create barcode.py file with content:

from barcode import EAN

Run file and see error:

$ python barcode.py
Traceback (most recent call last):
  File "barcode.py", line 1, in <module>
    from barcode import EAN
  File "C:\Users\riedelc\projects\zebra-printer\barcode.py", line 1, in <module>
    from barcode import EAN
ImportError: cannot import name 'EAN' from partially initialized module 'barcode' (most likely due to a circular import) (C:\Users\riedelc\projects\zebra-printer\barcode.py)

Example 2

Create barcode.py file with content:

from barcode import codex

Run file and see error:

$ python barcode.py 
Traceback (most recent call last):
  File "barcode.py", line 1, in <module>
    from barcode import codex
  File "C:\Users\riedelc\projects\zebra-printer\barcode.py", line 1, in <module>
    from barcode import codex
ImportError: cannot import name 'codex' from partially initialized module 'barcode' (most likely due to a circular import) (C:\Users\riedelc\projects\zebra-printer\barcode.py)

Example 3

Create barcode.py file with content:

from barcode.codex import EAN

Run file and see error:

$ python barcode.py
Traceback (most recent call last):
  File "barcode.py", line 1, in <module>
    from barcode.codex import EAN
  File "C:\Users\riedelc\projects\zebra-printer\barcode.py", line 1, in <module>
    from barcode.codex import EAN
ModuleNotFoundError: No module named 'barcode.codex'; 'barcode' is not a package

Additional

Screenshot of file tree: image As you can see the package is installed as it should be.

WhyNotHugo commented 2 years ago

Is "C:\Users\riedelc\projects\zebra-printer\ in your $PYTHONPATH? It seems that import barcode is importing your module (e.g.: barcode.py). So it just tries to import itself in a loop.

Cielquan commented 2 years ago

Embarrassing. 🤦

Yes of cause, the directory is in the PYTHONPATH, because I run the python command from within this directory and thus my custom barcode.py clashes with the python-barcode's namespace.

Thanks very much for the hint and sorry.