Spyder imports numpy from the wrong python environment - python

I want to import opencv in spyder using the IPython console. However, when I try to do this, I get an import error on numpy.
Platform: Jetson xavier
Spyder version: 3.2.6
I'm using 'virtualenv' to manage my virtual environment.
What works:
I can import cv2 in python using a bash terminal. Steps to reproduce:
Activate my virtual environment by running source my_env/bin/activate
Start python by running python
Import opencv by running import cv2
Success
I use spyder as my IDE and I want to import opencv in a python script run by spyder. However, when I run import cv2 I get ImportError: numpy.core.multiarray failed to import. Steps to reproduce:
In spyder in preferences set the Python interpreter to the one from my virtual environment (~/my_env/bin/python3)
Restart IPython console
Type import cv2 in the IPython console
I get the ImportError: numpy.core.multiarray failed to import
I already tried updating numpy, as recommended by ImportError: numpy.core.multiarray failed to import but this did not work.
Update
In spyder I can import numpy manually, and using inspect.getfile(numpy) tells me that spyder imported numpy from /usr/lib/python3/dist-packages/numpy/__init__.py, While it sohuld import numpy from ~/my_env/lib/python3.6/site-packages/numpy. Why is spyder importing numpy from the wrong environment?

Related

Jupyter Notebook creates new line instead of running block

I recently downloaded Jupyter Notebook to run some simple python code but whenever I try to run it by pressing shift and enter, all it does is give me a new line. I tried to run
import sys
import tensorflow.keras
import pandas as pd
import sklearn as sk
import tensorflow as tf
import platform
print(f"Python Platform: {platform.platform()}")
print(f"Tensor Flow Version: {tf.__version__}")
print(f"Keras Version: {tensorflow.keras.__version__}")
print()
print(f"Python {sys.version}")
print(f"Pandas {pd.__version__}")
print(f"Scikit-Learn {sk.__version__}")
gpu = len(tf.config.list_physical_devices('GPU'))>0
print("GPU is", "available" if gpu else "NOT AVAILABLE")
into a notebook, but it doesn't work. I also tried to run it on vs code but when I do, the terminal says...
import tensorflow.keras
ModuleNotFoundError: No module named 'tensorflow'
Please help! I have no clue what I am supposed to do!
If you install "tensorflow" in Jupyter Notebook
I will NOT be installed in python environment.
If you want to run it in vscode. You ought to install "tensorflow" with pip in the python environment.
Just open the "cmd" (not the one in Jupyter) and use pip to install
pip install tensorflow

jupyter notebook fail to import gdal while normal terminal python can

I got gdal installed successfully on my mac big sur.
the following command runs fine on my terminal
which python3
# /usr/local/bin/python3
python3
then I can import gdal in python console
from osgeo import gdal
and then I opened vscode and Jupyter notebook, Jupyter told me I'm using /usr/local/bin/python3 as the kernel, but when i execute in Jupyter
from osgeo import gdal
it fails with an importerror
ImportError: dlopen(/usr/local/lib/python3.9/site-packages/osgeo/_gdal.cpython-39-darwin.so, 2): Symbol not found: _GEOSArea
Referenced from: /usr/local/opt/libspatialite/lib/libspatialite.7.dylib
Expected in: flat namespace
in /usr/local/opt/libspatialite/lib/libspatialite.7.dylib
also, I always start Jupyter notebook via right click -> run current file in interactive Window. It gives me an import error immediately, but if I restart the kernel and type the following in interactive mode
from osgeo import gdal
It works fine, and it confuses me a lot.
edit
the developer of georasters, ozak asked this question on github
https://github.com/geopandas/geopandas/issues/214
and the developer of fiona answered it.
https://github.com/Toblerity/Fiona/issues/92
long story short. Fiona package is not compatible with osgeo.*

ModuleNotFoundError: No module named 'websocket' even though I installed pip install websocket

I am trying to run some code that has 'import websocket' however I am getting the error: ModuleNotFoundError: No module named 'websocket'
I have Python 3.7.3 and I am running in Spyder (if that makes a difference).
So from other questions/answers I found on here, in my cmd I ran pip install websocket and then also pip install websocket-client when the first one didn't run.
I am still getting the ModuleNotFoundError. Does it matter the location/folder of the code or where I install the pip command in cmd?
My python code starts with these import statements:
import json
import websocket
import traceback
import helper
import ssl
import time as time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
from mpl_toolkits.mplot3d import Axes3D
In cmd I ran:
C:\Users\myname>pip install websocket
and also:
C:\Users\myname>pip install websocket-client
The error I am getting is:
File "C:/Users/micki/Downloads/Derbit-Volatility-Visulization-master/Derbit-Volatility-Visulization-master/Volatility Surface Class.py", line 2, in <module>
import websocket
ModuleNotFoundError: No module named 'websocket'
Not sure, as you did not cover how you installed and are using Spyder, though I think it is probably an issue with your environment. You might also find that you are missing the module "helper" as well. There's two easy options as follows:
If you installed and are using Spyder via conda or anaconda, follow their documentation on installing websocket-client to the correct environment found here.
The second option (the preferred option IMHO, as you can use any IDE or text editor going forward), regardless of how you installed Spyder, would be to create a python virtual environment python3 -m venv /path/to/new/virtual/environment, pip install all your dependencies in said environment, then link Spyder's interpreter to the interpereter that was installed when you made the environment. In Spyder, go to Tools -> Preferences -> Python interpreter -> check the "Use the following Python interpreter:" radio button and enter the path to the interpreter from the environment you just created. For reference, see docs on making and using a python venv here.
If the websocket and websocket-client doesn't work, try:
pip install websocket_client
This solved my issue:
sudo pip install websocket-client

Issue with import 'logbook'

I installed logbook with my terminal using
conda install -c quantopian logbook and It works just fine when I try to use it in a python script file.py but once I try to run the same code on a jupyter notebook, It throws error so as:
2
----> 3 import logbook
4 import tempfile
5 import numpy as np
ModuleNotFoundError: No module named 'logbook'
Furthermore, the notebook and the installation was in same folder, If that does change anything. Also the python version used in 3.6.4
Looking forward to some help here!
thanks

ModuleNotFoundError: python import extern module

I struggle with importing Pillow in my python code.
My import statements:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import pillow
I use an local Anaconda interpreter (Anaconda 3) where I installed pillow:
conda install -c anaconda pillow
If I check with conda list there is a pillow-package...
Pycharm still throws: ModuleNotFoundError: No module named 'pillow'
There were no errors during pillow installation...
I tried restarting pycharm, reboot, uninstall / reinstalling pillow
I have no further idea what I can do, would be great if someone can help me :)
I'm working with pycharm 2017.3 on Windows 7 (64bit)
Kind regards, Lukas

Categories