poetry: Tensorflow 2.13.0 cannot be imported after install
- Poetry version: Poetry (version 1.5.1)
- Python version: Python 3.11.4
- OS version and name: macOS Ventura 13.3 M2 Chip
macOS-13.3-arm64-arm-64bit - pyproject.toml: pyproject.toml
- I am on the latest stable Poetry version, installed using a recommended method.
- I have searched the issues of this repo and believe that this is not a duplicate.
- I have consulted the FAQ and blog for any relevant entries or release notes.
- If an exception occurs when executing a command, I executed it again in debug mode (
-vvvoption) and have included the output below.
Issue
After installing tensorflow 2.13.0 with above pyproject.toml on macOs Apple Sillicon , importing fails with
ModuleNotFoundError: No module named 'tensorflow
Same method works under linux. HINT: in tensorflow 2.13 the authors introduce platform specific install for pip. Under macOs arm it is supposed to install tensorflow-macos . under linux there is another dependency tree
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 11
- Comments: 32 (8 by maintainers)
I found the solution: Wirg’s solution actually specified the wrong platform for pytorch-intel, one doesn’t have to write windows, but win32. Here is a corrected version:
For people still looking for a full combo workaround, this should work for tensorflow 2.11 in all situations (in reality I haven’t tested everything). Don’t hesitate to comment with your problems so that we improve on this. I’ve tested :
This should work with windows, new mac, old mac, docker in new mac, amd and non-amd linux machines … EDIT : Tested :
Currently, I advise to copy the full code below in your
pyproject.tomlin the poetry’s dependencies. Using a poetry group,[tool.poetry.group.tensorflow.dependencies]section could be a good thing to be able to isolate it :EDIT : I fixed above the tensorflow-io-gcs-filesystem issue from @RRiva EDIT 2 : adding @radoering answer EDIT 3 : fixing windows from @Vuizur
@dre-hh Have using environment markers to get around the issue?
If we take a look at tensorflow/tools/pip_package/setup.py, we see that they require the tensorflow-macos package where the environment marker has been satisfied.
It’s generated, but would look something like this.
tensorflow-macos == 2.15.0 ;platform_system=="Darwin" and platform_machine=="arm64"Following the Poetry documentation here, your dependency specification should look this.
I’d tend to agree with @dimbleby, the devs over at @tensorflow should improve the compatibility of their project with package managers, but that’s another battle entirely.
On windows, Also should do
poetry.exe add tensorflow-inteloperation seperately. And thenimport tensorflowwill work fine.The issue has been resolved on Windows: please refer https://github.com/tensorflow/tensorflow/issues/62899#issuecomment-1935052936
https://github.com/python-poetry/poetry/issues/8271#issuecomment-1928038659
Telling users that they should manually add the dependencies that tensorflow forgot to declare is at best a workaround, not a resolution.
#1616 is about different path dependencies with the same version (and resolved), thus, not related.
It’s related to #2012 and #4046. If there is no wheel for Windows (and no sdist), you’ll want to exclude that version for Windows:
this optimism seems to have been unwarranted, tensorflow have now rearranged things significantly - and they are once again providing completely different metadata in different distributions of their most recent release.
eg compare the output of
curl -s https://files.pythonhosted.org/packages/71/a2/863a553d48f52d8f719626bb6d1c6f51d77e26d4bb9445a48fbf73f8e445/tensorflow-2.16.1-cp312-cp312-macosx_12_0_arm64.whl.metadata | grep Requires-Distwithcurl -s https://files.pythonhosted.org/packages/76/4f/39ddae9fb07b8c039fa5a5f2b6623c6e0564199d82da33fcef62bcf93174/tensorflow-2.16.1-cp312-cp312-win_amd64.whl.metadata | grep Requires-Distto be clear: this variation is (unfortunately!) allowed by the packaging specifications, if being poetry-compatible is more trouble than it is worth to tensorflow users and contributors then so be it.
But this is not and almost certainly never will be compatible with installation through poetry.
I’m having this same issue with
poetryandtensorflowonly.poetry run pip listshows many things and those things import in the python REPL, but nottensorflow.I do not have any of these packages installed globally. So, I know my environment is fine. I even did
poetry env remove [the name]and did apoetry installagain. Andpoetry cache clear . --all. I have no idea what is going on. Some interaction between poetry and tensorflow?ah, I see the issues linked at the top … I should have followed those first
Data point: Vuizur 's solution worked for me on M2 max.
tensorflow’s metadata does not express that dependency:
No mention here of
tensorflow-macos.I guess this is another variation on https://github.com/tensorflow/tensorflow/issues/58674
please encourage the tensorflow folk to publish consistent metadata in all of their wheels, with platform-specific variations described by markers