简体   繁体   中英

ValueError while installing and running Tensorflow

To learn about Deep Learning and Neural Networks, I am trying to install Tensorflow on my laptop (Dell Inspiron 5559, 4GB RAM, Windows 10), using conda 4.9.1 (which initially contains Python 3.8.3 ). For this, I used the following command:

conda create -n tf tensorflow

This ran successfully and it installed Python 3.7.9 , tensorflow 2.1.0 and a lot of other modules. Now I want to test whether it works or not, so in Python terminal, imported some modules. Import was successful for math and numpy . But when I import tensorflow, the process fails with the following:

>>> import tensorflow      

Traceback (most recent call last):                                                                                                                                            
File "<stdin>", line 1, in <module>                                                                                                             
File "D:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\__init__.py", line 101, in 
<module>                 from tensorflow_core import *                                                                                         
File "D:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\__init__.py", line 40, in    
<module>             from tensorflow.python.tools import module_util as _module_util                                                       
File "<frozen importlib._bootstrap>", line 983, in _find_and_load                                                       
File "<frozen importlib._bootstrap>", line 959, in _find_and_load_unlocked                                              
File "D:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\__init__.py", line 50, in 
__getattr__               module = self._load()                                                                                                 
File "D:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\__init__.py", line 44, in _load                     
module = _importlib.import_module(self.__name__)                                                                      
File "D:\Program Files\Anaconda3\envs\tf\lib\importlib\__init__.py", line 127, in import_module                           
return _bootstrap._gcd_import(name[level:], package, level)                                                           
File "D:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\__init__.py", line 
49, in <module>      from tensorflow.python import pywrap_tensorflow                                                                       
File "D:\Program Files\Anaconda3\envs\tf\lib\site- 
packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>                                                                                                                     
from tensorflow.python.pywrap_tensorflow_internal import *                                                            
File "D:\Program Files\Anaconda3\envs\tf\lib\site- 
packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>                                                                                                            
_pywrap_tensorflow_internal = swig_import_helper()                                                                    
File "D:\Program Files\Anaconda3\envs\tf\lib\site- 
packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 15, in swig_import_helper                                                                                                  
import imp                                                                                                          
ValueError: source code string cannot contain null bytes

This error occurs even if I run a program importing Tensorflow.

I checked most other tutorials and it seemed that nobody faced this error. I also tried using using lower versions of Python and Tensorflow but the same error occurs.

Can anyone find what is happening, and what should I do?

One possible solution is to downgrade the TensorFlow to 2.00. First, remove TensorFlow from Anaconda using conda remove tensorflow

and then downgrade it.

pip install --upgrade pip
pip install tensorflow==2.0

我发现使用不是 3.6.X 的 python 版本安装和导入 tensorflow 有时非常困难,我建议使用 python 3.6.X,它更容易

I believe your issue is that you installed tensorflow with conda, the conda build has some issues, try running the pip call in your conda env

conda remove tensorflow

then run

pip install --upgrade pip
pip install tensorflow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM