I'm having trouble to import TensorFlow in my python application, BUT only
if I'm running my application in VSCode (via the debugger) OR
if I'm running it from the Terminal WITHIN VSCode.
Everything is working fine if I'm running the application from the Terminal outside of VSCode.
I'm running VSCode on macOS Big Sur Version 11.1 (M1 chipset).
I have python 3.8.2 and TensorFlow installed in a virtual environment.
Here's the steps to reproduce the Error. From the Terminal outside of VSCode I run
source env/bin/activate to activate the virtual environment
python to launch python. Output to the terminal (as expected): Python 3.8.2 (default, Nov 4 2020, 21:23:28) [...]
import tensorflow as tf
print(tf.__version__) This prints "2.4.0-rc0" to the terminal (as expected).
Now, if I repeat the exact same steps 1 and 2 in the built in VSCode terminal, I get the exact same output in 2. However, if I run command 3 and try to import tensorflow, the following error message shows up:
Traceback (most recent call last):
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found. Did find:
/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/__init__.py", line 39, in <module>
from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 83, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found. Did find:
/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/errors
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
It seems like the Terminal within VSCode is not using the same site-packages as the Terminal outside of VSCode, however, running print(sys.path) gives the same result.
The same problem happens if I'm trying to run my application within VSCode whereas it is working if I'm running it from the Terminal.
Any advice is greatly appreciated.
Try Python 3.7. Many people have complained tensorflow not working on 3.8, but working on 3.7. Also, try downloading it from the google tensorflow web page.
Piggy-backing off of Jay Mody in the comments above. I would first check that the shells you use in VS code is the same as the shell you are using in the non-VS code terminal.
Try the following shell commands:
echo $SHELL
If you get the same output, then I would suggest making sure the exact same python executables are being used. Try typing this in both of the shells:
which python
In VS Code, open an integrated Terminal, after activating the environment, run
pip show tensorflow
to check if the module exists in the current environment. If not, reinstall it.
I still don't know why this problem existed in the first place but I now have it resolved by installing python 3.8 via the ARM build of mini-conda.
Here are the steps.
Download mini-conda from here https://conda-forge.org/blog/posts/2020-10-29-macos-arm64/ and install it.
After installation, create a new Conda environment conda create --name python38 python=3.8. This will install the ARM version of python 3.8
Activate the new environment conda activate python38
Create a new virtual environment for your project. python -m venv myEnv
Download and unpack the ARM version of tensorflow https://github.com/apple/tensorflow_macos/releases
Run the included script to install tensorflow. /Volumes/SSD/Jan/Downloads/tensorflow_macos/install_venv.sh --prompt and point it to your newly created virtual environment of step 4.
With these steps, tensorflow could be correctly imported.
Some more reference which pointed me to this solution:
https://github.com/apple/tensorflow_macos/issues/8
https://github.com/apple/tensorflow_macos/issues/3
Related
I am trying to use NumPy in Python. I have just installed Anaconda Python 3.7, and that all seemed to go smoothly. However, I cannot import numpy(using the line import numpy). When I do, I get the following error:
C:\Users\jsmith\anaconda3\lib\site-packages\numpy\__init__.py:140: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
from . import _distributor_init
Traceback (most recent call last):
File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\core\__init__.py", line 24, in <module>
from . import multiarray
File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\core\multiarray.py", line 14, in <module>
from . import overrides
File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
from numpy.core._multiarray_umath import (
ImportError: DLL load failed: The specified module could not be found.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import core
File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\core\__init__.py", line 54, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
1. Check that you expected to use Python3.7 from "C:\Users\jsmith\anaconda3\python.exe",
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy version "1.18.1" you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was:
DLL load failed: The specified module could not be found.
I can see it in the Enviorments tab of Anaconda Navigator, and when I try to use it in Eclipse(Pydev) it shows up under forced builtins. I took a look at my PYTHONPATH, and both my enviorment in Eclipse and my base python directory (jsmith/anaconda3) are in it. I have tried importing other libraries I see under forced builtins,and those work fine, yet numpy seems to be the only one with issues. Calling pip install numpy tells me it is already installed with version 1.18.1. I looked at this stack overflow page, and ran the first command in the answer(conda create -n test numpy python=3.7 --no-default-packages) in anaconda prompt. This worked, and then I realized the test was specific to the question, and tried base instead, and got this error:
CondaValueError: The target prefix is the base prefix. Aborting.
However calling conda activate base did nothing.
As mentioned in the comments by #cel uninstalling and reinstalling numpy using pip uninstall numpy and pip install numpy made it work.
I better way is to
import os
import sys
os.path.dirname(sys.executable)
This will give you the path to your environment. Put the path into the settings for python
Open the Anaconda Prompt:
Then, you have to go to the Conda Environment that you want to use in PowerBI. Am having an environment 'temp', so I activate it first in the 'Anaconda Prompt':
(base) C:\Users\ashish>conda activate temp
Then I go to the directory having the "PowerBI" executable file in the installation folder:
(temp) C:\Users\ashish>cd "C:\Program Files\Microsoft Power BI Desktop\bin"
Then, I launch PowerBI from the Prompt:
(temp) C:\Program Files\Microsoft Power BI Desktop\bin>PBIDesktop.exe
This fixes the NumPy error you are getting. If you want any other package to use with PowerBI, install that package in the respective "Conda Environment" (in my case it is "temp").
Make sure the Python home directory (Anaconda3) has been added to the 'Power BI Desktop' global options in the Python scripting section too.
When I use Anaconda, importing numpy gives me the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File"C:\Users\s140041\Anaconda3\envs\testenv\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File"C:\Users\s140041\Anaconda3\envs\testenv\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Users\s140041\Anaconda3\envs\testenv\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File"C:\Users\s140041\Anaconda3\envs\testenv\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File"C:\Users\s140041\Anaconda3\envs\testenv\lib\site-packages\numpy\core\__init__.py", line 26, in <module>
raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: The specified module could not be found.
I have already done a LOT of troubleshooting:
I do not think the issue is path-related, Since Anaconda is my only installation of python on this machine, and the problem persists after a fresh installation of windows. I have not added anaconda to Path right now, since I just use the Anaconda prompt for all my conda related commands.
I do not think it is a problem with the environments, Problem exists on base/root environment of conda as well as on a new conda environment.
Problem is not system-related, was able to reproduce it on 2 seperate systems with specified version.
Problem is not PyCharm related, running python.exe from the root folder also gives the same error.
Uninstalling / reinstalling through conda doesn't work.
Conda does seem to look in the right library for the packages, checked this through print(sys.path) in python.exe, it returns the local library of anaconda where all it's default packages are installed.
Only when I uninstall using conda and re-install using pip everything works fine... Am i doing something wrong here, or is conda broken in this installation of Anaconda?
I just switched from a normal python/venv/pip to Anaconda/conda/environments, but I must say this is not really a warm welcome... Even though I heard that the conda packagement system is better than pip/venv.
UPDATE:
I filed a bug report on github, which can be found here.
https://github.com/conda/conda/issues/7833
Very clear discussion of what went wrong can be found in the bug-report on GitHub:
https://github.com/conda/conda/issues/7833
Summarized, using PyCharm with an unactivated environment is unsupported. So either use anaconda prompt, activate your environment and launch PyCharm from there, or re-install anaconda/mini-conda with the option "add-to-path" checked
Python was working file in my machine until yesterday night.
When I run my spyder I get,
An error occurred while starting the kernel
Error in sitecustomize; set PYTHONVERBOSE for traceback:
ImportError: DLL load failed: Access is denied.
If I try to install any module or import I get the below error,
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\__init__.py", line
22, in <module>
from pandas.compat.numpy import *
File "C:\ProgramData\Anaconda3\lib\site-
packages\pandas\compat\__init__.py", line 36, in <module>
from unicodedata import east_asian_width
ImportError: DLL load failed: Access is denied.
I found this is happening because of _socket.py , I tried changing _socket.pyd from another working machine but still, I could not fix this issue.
Please help, thanks in advance!
It appears that there are some other users having similar problems with kernel restart.
See here https://github.com/spyder-ide/spyder/issues/2812
Although it is difficult to see whether the above would help - maybe you could provide a way to replicate this problem?
In above thread the solution proposed is to run
conda update setuptools
An important element is always to check if you are in the correct environment or if you always work within an environment. This normally prevents many kernel and spyder issues as well as lost dependencies
You can create a new environment and reopen spyder from within the activated environment
conda create --name newEnv python=3.5
activate newEnv (WINDOWS)
source activate newEnv (MACOS/LINUX)
then
conda install anaconda
Additionally, what has helped me in the past is to go to tools-> reset spyder to factory defaults
When I try to import the slycot module in spyder (version 2.2), I get the following error:
RuntimeError: module compiled against API version 7 but this version of numpy is 6
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/slycot/__init__.py",
line 4, in <module>
from slycot.analysis import ab01nd,ab05md,ab05nd,ab07nd,ab08nd, ab09ad
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/slycot/analysis.py",
line 21, in <module>
from slycot import _wrapper
ImportError: numpy.core.multiarray failed to import
Now, when I import slycot in the python shell through my terminal, there are no problems at all. I think the reason why there are no problems through the terminal, is that I recently installed numpy 1.7.1. Spyder on the other hand still uses numpy 1.6.2.,hence the error.
I tried to change the numpy version in spyder, but so far that didn't work.
When I change the numpy(6) folder in my spyder lib with the recently installed numpy(7), I get the following error (in spyder):
ImportError: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python intepreter from there.
What is the best way to solve this error?
Thanks in advance.
(Spyder dev here) At the moment it's not possible for the user to install anything inside the app. I guess you moved your slycot module from another interpreter to our App (because of the numpy error you mention).
The right way to use different modules not present in the app, is to change the path of your interpreter in:
Tools > Preferences > Console > Advanced settings > Python executable
If you installed slycot using your system Python, you have to select
/usr/bin/python
I'm trying to use VirtualEnv for a Flask app that I'm creating since everyone has recommended me to do so. After creating my virtual environment, I installed the libraries that I needed using pip while the environment was activated. I'm running into ImportError problems with this script. The code works fine when I'm not in the virtual environment.
My script:
#!/usr/bin/python
import sc2reader
...
...
When I try to run it I get this:
(flaskapp)xxxx#xxxx-VirtualBox:~/flaskapp/bin$ ./test.py
Traceback (most recent call last):
File "./test.py", line 3, in <module>
import sc2reader
ImportError: No module named sc2reader
I've tried changing the shebang to reflect my VirtualEnv path for Python, but that didn't fix anything. The library is found in my site-packages folder in my virtual environment, so I'm not sure why I'm getting the ImportError.
I've never used VirtualEnv before so I'm assuming I configured it wrong so it's not seeing my site-packages.
Try using
#!/usr/bin/env python
as the shebang.
If that does not work, try seeing what is the output of which python.