gtalarico / pyairtable

Python Api Client for Airtable
https://pyairtable.readthedocs.io
MIT License
786 stars 139 forks source link

ImportError: cannot import name 'Table' from 'pyairtable' (unknown location) #191

Closed NicoHood closed 2 years ago

NicoHood commented 2 years ago

I currently try to modify pyairtable, so I've installed it locally via venv. However using the ORM fails with:

ImportError: cannot import name 'Table' from 'pyairtable' (unknown location)

https://github.com/gtalarico/pyairtable/blob/main/pyairtable/orm/model.py#L64

The solution is to use from pyairtable.api import Table here. I dont know if that is a correct patch, and I am not so familiar with python imports. Maybe you can help with that.

NicoHood commented 2 years ago

How would you properly install pyairtable in a local development environment? I already have a venv for my script setup. I tried to cd into pyairtable and ran make setup. but i then cd out of that directory I always get this error. I have no idea how to fix it properly and right now (outside the orm) the code does not run at all. I am a bit lost. can you help?

gtalarico commented 2 years ago

@NicoHood

Here is how I usually do it

~/dev/repos/gtalarico/pyairtable
❯ python -m venv .venv

~/dev/repos/gtalarico/pyairtable 
❯ source .venv/bin/activate.fish   [ Varies by shell used ]

~/dev/repos/gtalarico/pyairtable
.venv ❯ pip install -e .
Obtaining file:///Users/gtalarico/dev/repos/gtalarico/pyairtable
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Collecting requests>=2
  Downloading requests-2.28.1-py3-none-any.whl (62 kB)
     |████████████████████████████████| 62 kB 1.8 MB/s
Collecting charset-normalizer<3,>=2
  Downloading charset_normalizer-2.1.1-py3-none-any.whl (39 kB)
Collecting idna<4,>=2.5
  Downloading idna-3.4-py3-none-any.whl (61 kB)
     |████████████████████████████████| 61 kB 329 kB/s
Collecting certifi>=2017.4.17
  Downloading certifi-2022.9.24-py3-none-any.whl (161 kB)
     |████████████████████████████████| 161 kB 15.3 MB/s
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.12-py2.py3-none-any.whl (140 kB)
     |████████████████████████████████| 140 kB 60.3 MB/s
Installing collected packages: charset-normalizer, idna, certifi, urllib3, requests, pyairtable
  Running setup.py develop for pyairtable
Successfully installed certifi-2022.9.24 charset-normalizer-2.1.1 idna-3.4 pyairtable requests-2.28.1 urllib3-1.26.12
WARNING: You are using pip version 20.2.3; however, version 22.3 is available.
You should consider upgrading via the '/Users/gtalarico/dev/repos/gtalarico/pyairtable/.venv/bin/python -m pip install --upgrade pip' command.

~/dev/repos/gtalarico/pyairtable rc-1.4.0*
.venv ❯ python -c "from pyairtable import Table; print(Table)"
<class 'pyairtable.api.table.Table'>
NicoHood commented 2 years ago

It still does not work for me. I have no idea why. I cannot develop this locally. And I have lots of things to improve, and I would create PRs for that, but I am unable to get this working :(

NicoHood commented 2 years ago
$ pip uninstall pyairtable 
Found existing installation: pyairtable 1.3.0
Uninstalling pyairtable-1.3.0:
  Would remove:
    /home/user/Documents/.venv/lib/python3.10/site-packages/__editable__.pyairtable-1.3.0.pth
    /home/user/Documents/.venv/lib/python3.10/site-packages/__editable___pyairtable_1_3_0_finder.py
    /home/user/Documents/.venv/lib/python3.10/site-packages/pyairtable-1.3.0.dist-info/*
Proceed (Y/n)? 
  Successfully uninstalled pyairtable-1.3.0

$ pip install -e .
Obtaining file:///home/user/Documents/pyairtable
  Installing build dependencies ... done
  Checking if build backend supports build_editable ... done
  Getting requirements to build editable ... done
  Preparing editable metadata (pyproject.toml) ... done
Requirement already satisfied: requests>=2 in /home/user/Documents/.venv/lib/python3.10/site-packages (from pyairtable==1.3.0) (2.28.1)
Requirement already satisfied: certifi>=2017.4.17 in /home/user/Documents/.venv/lib/python3.10/site-packages (from requests>=2->pyairtable==1.3.0) (2022.6.15.1)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/user/Documents/.venv/lib/python3.10/site-packages (from requests>=2->pyairtable==1.3.0) (1.26.12)
Requirement already satisfied: idna<4,>=2.5 in /home/user/Documents/.venv/lib/python3.10/site-packages (from requests>=2->pyairtable==1.3.0) (3.3)
Requirement already satisfied: charset-normalizer<3,>=2 in /home/user/Documents/.venv/lib/python3.10/site-packages (from requests>=2->pyairtable==1.3.0) (2.1.1)
Building wheels for collected packages: pyairtable
  Building editable for pyairtable (pyproject.toml) ... done
  Created wheel for pyairtable: filename=pyairtable-1.3.0-0.editable-py2.py3-none-any.whl size=3860 sha256=f4b732c19478c1444cffa13b857ee31a3bffff358c5732d09701a87086c0207b
  Stored in directory: /tmp/pip-ephem-wheel-cache-de0ksvrg/wheels/3e/da/83/cba84ba2d32bffb5c1ffce6529e07853e8dd5866091f0258c9
Successfully built pyairtable
Installing collected packages: pyairtable
Successfully installed pyairtable-1.3.0

[notice] A new release of pip available: 22.2.1 -> 22.3
[notice] To update, run: pip install --upgrade pip

$ python -c "from pyairtable import Table; print(Table)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'Table' from 'pyairtable' (unknown location)
NicoHood commented 2 years ago

If you look at the installed package in the venv, the directory is quite empty compared to others: grafik

Edit: This should be because we used the -e option. But still no clue why it is not working

NicoHood commented 2 years ago

I got it working by installing it without the -e flag: pip install ./pyairtable

However it would be nice to understand why.

NicoHood commented 2 years ago

Maybe the init.py is wrong? It does not help, but I want to note it here: https://packaging.python.org/en/latest/guides/packaging-namespace-packages/#native-namespace-packages

gtalarico commented 2 years ago

Do you have a file or folder named "pyairtable" from where you are running the commands from?

NicoHood commented 2 years ago

no, but i have cloned pyairtable into a folder of that name. This is the git default

gtalarico commented 2 years ago

I have no doubt this error is because of how & where you are executing your code from and local files clashing.

There is the only possible way it would import pyairtable but not find Table. Adding .api fixed because there no clash.

After you install you should be running python from inside the package folder. Being one folder up might be causing the conflict (because the repo folder name and the package name inside are both named pyairtable, which is a common pattern for packages organization unfortunately)

You can read more about details around name resolution here: https://stackoverflow.com/questions/16245106/python-import-class-with-same-name-as-directory

If you search our issues for import error you will see many instances of similar issues.

Try doing exactly as I should above (install using '.' and should work because you will be inside the package and not one folder up as in your example which causes the clash (pyairtable x pyairtable/pyairtable)

NicoHood commented 2 years ago

Nice! That did the trick! (I renamed the git clone pyairtable to pyairtable_git)