我的 Python 版本是 2.7.13。
我在 Jupyter Notebook 中运行以下命令。
首先,我安装了软件包
%%bash
pip uninstall -y google-cloud-dataflow
pip install --upgrade --force tensorflow_transform==0.15.0 apache-beam[gcp]
然后,
%%bash
pip freeze | grep -e 'flow\|beam'
我可以看到包 tensorflow-transform 已安装。
apache-beam==2.19.0
tensorflow==2.1.0
tensorflow-datasets==1.2.0
tensorflow-estimator==2.1.0
tensorflow-hub==0.6.0
tensorflow-io==0.8.1
tensorflow-metadata==0.15.2
tensorflow-probability==0.8.0
tensorflow-serving-api==2.1.0
tensorflow-transform==0.15.0
但是,当我尝试导入它时,会出现警告和错误。
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow_estimator/python/estimator/api/_v1/estimator/__init__.py:12: The name tf.estimator.inputs is deprecated. Please use tf.compat.v1.estimator.inputs instead.
ImportErrorTraceback (most recent call last)
<ipython-input-3-26a4792d0a76> in <module>()
1 import tensorflow as tf
----> 2 import tensorflow_transform as tft
3 import shutil
4 print(tf.__version__)
ImportError: No module named tensorflow_transform
经过一番调查,我想我对这个问题有了一些想法。我运行这个:
%%bash
pip show tensorflow_transform| grep Location
这是输出
Location: /home/jupyter/.local/lib/python3.5/site-packages
我尝试通过将 /home/jupyter/.local/lib/python3.5/site-packages 添加到 $PATH 的开头来修改 $PATH。但是,我仍然无法导入 tensorflow_transform。
基于以上和以下信息,我认为,当我运行导入命令时,它执行的是 Python 2.7,而不是 Python 3.5
import sys
print('\n'.join(sys.path))
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/usr/local/lib/python2.7/dist-packages/IPython/extensions
/home/jupyter/.ipython
还,
import sys
sys.executable
'/usr/bin/python2'
我认为问题是 tensorflow_transform 包安装在 /home/jupyter/.local/lib/python3.5/site-packages 中。但是当我运行“导入”时,它会转到 /usr/local/lib/python2.7/dist-packages 来搜索包,而不是 /home/jupyter/.local/lib/python3.5/site-packages ,所以即使更新 $PATH 也无济于事。我对吗?
我试图升级我的python,但是
%%bash
pip install upgrade python
Defaulting to user installation because normal site-packages is not writeable
然后,我添加了 --user。看来python并没有真正升级。
%%bash
pip install --user upgrade python
%%bash
python -V
Python 2.7.13
有什么解决办法吗?