ModuleNotFoundError despite package installation - python

For an unknown reason, some functions of the Python package GeoPandas were not working on my PC any more. I therefore deinstalled and reinstalled Anaconda and also installed the Python package GeoPandas via conda create -n geo-env -c conda-forge geopandas which is now check-marked as installed in the Anaconda package overview.
However, when I run import geopandas in Spyder I receive the following error:
runfile('untitled0.py', wdir='/Downloads')
Traceback (most recent call last):
File "untitled0.py", line 1, in <module>
import geopandas
ModuleNotFoundError: No module named 'geopandas'
Does anyone have an idea what I can do? I made sure I am running the correct Anaconda environment.

Not familiar with Spyder and haven't used Anaconda much, but I got curious. The first thing is that you want to install the Spyder-kernel as described here in your geo-env environment (you might already have done that). Great, all done, you load up Spyder, change your runtime to the correct python in your environment and... It doesn't work.
From my testing, Spyder is not very dynamic, so I had to restart Spyder once I had set it up to the correct environment. On restart, you will notice that the iPython window attempts to load the kernel and then it will work.

Related

Why I can't import cv2 and other modules to python

I have problem with importing some modules into python.
First of all, I am on MacBook, it's my first OS X device, so maybe I did something wrong.
I installed conda, created main. Everytime I want to work on my python app, I just open terminal, use command: conda activate main, cd into folder and open in VS code. I used pip3 install opencv-python to download this module, It was completed without errors but when I import everything into python file (cv2, tensorflow, matplotlib.pyplot and numpy) and I just
try to print Hello, I get this error:
Traceback (most recent call last):
File "/Users/zacikm/dev/Python/BC/main.py", line 1, in <module>
import cv2 as cv
ModuleNotFoundError: No module named 'cv2'
Please, help me what can I do to fix this problem. I want to start working on my school project but I'm not able.
Thanks!
Sure.
Picture when I run "pip install opencv-python" under conda env.
Error message what I get after running my code.
Check are you using the right environment after installing the pkg pip install opencv-python.
For example, you are using base python rather the conda one.
Say your conda environment is called test.
You need to run
conda activate test
Then use
pip install opencv-python
to re-install it for this environment. Then you can use cv2 in this test environment.
I had the same problem. I found that the solution was to downgrade the opencv version. In Pycharm, the process is as follows:
In Preferences, go to Python interpreter. You will see the current versions of opencv-contrib-python and opencv-python which may be the same as the latest version.
That is the problem.
Double click on the current version of opencv-contrib-python to open this screen:
Check the specify version checkbox and find version 4.1.2.30. Then click Install Package and wait a few seconds to get a message that package was successfully installed. Press OK and you should see opencv-conbtrib-python downgraded to the appropriate version.
After step 3, opencv-python may have also downgraded to the appropriate version 4.2.0.34. If it hasn't, follow the same process to downgrade opencv-python to 4.2.0.34. At the end, your python interpreter screen should look like this:
That should do it. Test it to make sure it works.

Importing the numpy c-extensions failed

Importing the numpy c-extensions failed
I installed python 3.7 on my windows system to work on visual studio code. Everything was going well, including using the libraries.
I uninstalled python using the uninstalling program tool in control panel. And installed Miniconda 3.
I checked that everything works well, and then installed the numpy library using conda install numpy in my terminal GitBash on windows 10, then checked it on my visial studio code, but it failed to start.
Reproducing code example:
import numpy as np
A = np.array([[-1], [7], [-26]])
Error message:
Traceback (most recent call last): File
"C:\Users\ramim\Miniconda3\lib\site-packages\numpy\core__init__.py",
line 17, in
from . import multiarray File "C:\Users\ramim\Miniconda3\lib\site-packages\numpy\core\multiarray.py",
line 14, in
from . import overrides File "C:\Users\ramim\Miniconda3\lib\site-packages\numpy\core\overrides.py",
line 7, in
from numpy.core._multiarray_umath import ( ImportError: DLL load failed: Не найден указанный модуль.
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"c:/Users/ramim/Desktop/22/Matrix library/alsf.py", line 3, in
import numpy as np File "C:\Users\ramim\Miniconda3\lib\site-packages\numpy__init__.py", line
142, in
from . import core File "C:\Users\ramim\Miniconda3\lib\site-packages\numpy\core__init__.py",
line 47, in
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\ramim\Miniconda3\python.exe",
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy version "1.17.3" 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: Не найден указанный модуль.
Numpy/Python version information:
Python 3.7.5
Numpy 1.17.3
I tried to uninstall and install the numpy library again, but it was useless.
Note: when I type in the teminal conda install numpy, it says:
All requested packages already installed
That's how I checked if numpy is really installed!
How to solve that?
Try to uninstall numpy and setuptools first:
pip uninstall -y numpy
pip uninstall -y setuptools
pip install setuptools
pip install numpy
Borrowed from solution provided by mehdiHadji here- https://github.com/ipython/ipyparallel/issues/349
Not sure this is a thing in Visual Studio too, but for Eclipse I had to change one of the environmental variables.
Setup: Windows, Python 3.7, Conda venv with numpy
Solution:
CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1
For Eclipse, the environment variables can be accessed via Properties -> Run/Debug Settings -> Edit -> Environment.
Anaconda also documented the fix, albeit for a different problem:
Conda Troubleshooting
Similar with my problem recently. I'm using Python 3.8 by Miniconda on Win 10 system. I solved the problem by changing default terminal from PowerShell to Command Prompt.
Steps:
Open VS Code's Command Palette menu by pressing Ctrl+Shift+P or F1
Choose "Terminal: Select Default Profile" entry
Then pick "Command Prompt" option
Restart VS Code
This issue is caused by VScode default terminal (powershell) setting,
To switch VScode default terminal from powershell to cmd, the conda env will be activated correctly, other powershell will try to invoke conda activate xxxxxx which will fail, then the subsequent import numpy will fail.
So two ways to fix it:
Fix path search issue under powershell environment to get conda activate xxxxxx successfully executed;
Change vs code default terminal to 'cmd':
add "terminal.integrated.shell.windows": "C:\Windows\System32\cmd.exe" in settings.json
I solved this by the following steps-
Uninstalling numpy and pandas with conda
Installing numpy and pandas with pip
I solved the problem by reinstalling NumPy through pip:
pip install --upgrade --force-reinstall numpy
I solved it by removing ALL numpy versions
pip uninstall numpy
pip3 uninstall numpy
And then installing numpy and libatlas-base-dev via apt-get
sudo apt-get remove python3-numpy
sudo apt-get install libatlas-base-dev
It looks like there's something wrong with your %PATH%. It might either contain some conflicts, or just be too long (>2047 chars). Try adding the folder with the dlls (from the environment you're trying to use) to the very beginning of it:
PATH=C:\Users\ramim\Miniconda3\Library\bin;%PATH%
python -c "import numpy"
(based on this)
For those scratching their head wondering why re-installing numpy module works:
If you are using a virtual environment (say in a relative location called env), make sure that folder does not make it to other platforms like Docker, or different OS you might be switching to.
Remember the compilation of Python and C extensions necessary for numpy are platform dependent. So if it works in Windows, it will not work in Linux using the same virtual environment (cached modules).
For example, this is particularly important if you are running your code on Windows - and then try to run it on a Linux container via Docker Desktop. (Make sure env - or whatever your virtual env is called - is ignored and not copied onto the container)
I had the same problem, and tried several of the solutions given here, but none worked for me. I looked in another forum and was able to solve the problem (https://github.com/numpy/numpy/issues/13252):
conda uninstall numpy
pip3 install numpy.
This issue is still ongoing. I use VS code with conda venv, and solved it in a similar way with marineCoder:
In addition to numpy and pandas, I also remove matplotlib using conda uninstall
Cautiously reinstall pandas and numpy using pip install
In my case, the error shows up whenever matplotlib package is installed, so I got to either remove it or downgrade the three of them. There is a clash on numpy dependency. Another related issue is shown in this post:
I get `No module named _multiarray_umath` when using matplotlib
In my case, I had to manually 'conda activate myenv' the desired environment in the VS code terminal. Previously, I only had to select the python version from the desired environment, and then the environment would auto-activate. This answer references and confirms Brett Cannon's comment above, which was the sole reason I thought to try it.
As I'm on MacOS (with an M1 CPU) I realize my answer is not exactly an answer to the poster's question but as I got the same error and Google sent me here perhaps this helps someone.
In my case I found the error was caused because VSCode was still running in Intel-X86 mode on my Mac-M1. Simply re-installing VSCode using the latest "Mac Universal" distribution switched it to ARM64 mode fixing the issue for me.
Uninstalled anaconda and installed latest version
I tried the answers here but they didn't work for me, uninstalled older version of anaconda (don't remember previous version number now) and installed Anaconda3-2022.05 (Anaconda Navigator 2.1.4). This sorted it for me. I hadn't used my anaconda setup in long time and thus didn't care if my preinstalled packages get wiped out. This took a lot less time and saved from pain of trying more approaches.
OS: Windows 10
Please check both of these (your PATH or PYTHONPATH environment variables) carefully to see if they are what you expect.
In my case, there was an issue related to updated versions of python and NumPy, I resolved both problems by running a single command over conda PowerShell prompt i.e. conda update --all
Here are the steps-
Open Anaconda Powershell Prompt and click 'run as Administrator'.
Enter the following code into the prompt and click enter: conda update --all ...
You are all set to run your script
I hit this error while running a block of code in a .ipynb Jupyter Notebook file.
I fixed the problem by switching the version of Python that was running. Specifically, I was running Python 3.8.5 from the Anaconda3 directory on my computer and I switched this to Python 3.9.2 that I had installed elsewhere on my computer.
To toggle the version of Python in VSCode while working on a .ipynb file, click on the version specified in the top-right corner of the screen. Thereafter, a dropdown menu will allow you to select from different versions of Python installed on your computer.
For my situation, I had updated the version of python that I was using. Running git clean -xdf resolved the issue.
My system:
OS: Mac OS 11.6
Python: 3.7.8 => 3.8.12
Numpy: 1.20.2
Poetry: 1.1.6

(Jupyter Notebook) ModuleNotFoundError: No module named 'pandas'

In my terminal, I ran:
pip install pandas
pip3 install pandas
Installation seemed to go well. When I write some code in a file and execute it in my terminal (prompting 'python filename.py' or 'python3 filename.py'), the pandas library can be imported and used without a problem. However, when using Jupyter Lab and Jupyter Notebook, and I get this error when trying to import pandas:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-38d4b0363d82> in <module>
----> 1 import pandas
ModuleNotFoundError: No module named 'pandas'
It seems like Jupyter Notebook does not recognize this library. Very confused as of why and what I should do. FYI reinstalling anaconda did not help, and I am using 'pip' and 'pip3' to install libraries.
I had a similar problem. Your best bet is to install your packages direct from Jupyter notebook, then you can be sure that the packages are being installed into the local python instance.
! pip install --user <package>
The ! tells the notebook to execute the cell as a shell command.
you've got at least 3 versions of Python installed (the system version, a copy of 3.7 and 3.8). you need to figure out which is which, i.e. what you've done to your system!
to know which version of Python is being run you can use something like (from your shell/command prompt, not in Python):
which python3
see here for an explanation and alternatives. this tells you where some version of Python is, but you can also ask for pip3 and conda, jupyter, etc, to see where those have ended up, and to make sure you're running the right one. note that this involves your "shell's path" which you can customise so it picks the right one
next you need tools to figure out the equivalent "within Python". Python libraries aren't independent programs/executables (i.e. what $PATH determines) so this is a seperate set of options
to display where Python looks for code you can do this (inside Python):
import sys
print(sys.path)
see here for more info about what's going on here
note that what happens when you "open jupyter notebook by clicking on the icon with Anaconda Navigator" is a bit more difficult to debug. sys.executable might be useful to figure out what's going on
if you know xkcd, we're sort of in this state

"No module named specutils" although specutils installation went through

I am trying to install the Specutils package.
I followed these instructions and typed the following command in my Anaconda Prompt terminal:
conda install -c astropy specutils
The installation went through. Typing conda list confirms that specutils 0.5.2 is installed.
However, trying to import specutils from Spyder does not work! I am getting a message stating that there is No module named specutils.
Any clue what I am doing wrong?
FYI, I am working from the "base" conda environment.
These problems are in most instances problems with the IDE. It does not pick up on the right Python environment that you installed stuff into. Verify this by running python from the command line and note the output of import sys;print(sys.prefix). Compare that to the one you are getting from within Spyder.

Actually opening spyder IDE

I've been trying to follow instructions on here and here to install spyder.
I downloaded the Source Code zip file from the first link because that's what I believe is the dmg installer. I don't know what to do with any of the files in the zip file once I download them though.
I already have pip3 installed so I used the command
pip3 install -U spyder
When I run the commmand
/User/Downloads/spyder-3.2.8/scripts/spyder
I get the error
Traceback (most recent call last):
File "/User/Downloads/spyder-3.2.8/scripts/spyder", line 2, in <module>
from spyder.app import start
ImportError: No module named spyder.app
To my understanding, spyder is supposed to be a python IDE that I am expecting to look something like this
I'm just trying to get the IDE running so that I can write my program within it to help me program easier. The only IDE i've used before is eclipse so maybe I'm not thinking of an IDE the right way, but I'm expecting something kind of like eclipse.
(Spyder maintainer here) I think our installation instructions are quite clear, but I repeat them here for you again:
If you are new to Python or the Scientific Python ecosystem, we strongly recommend you to install and use Anaconda. It comes with Spyder and all its dependencies, along with the most important Python scientific libraries (i.e. Numpy, Pandas, Matplotlib, IPython, etc) in a single, easy to use environment.
After installing Anaconda, you need to open the Anaconda Navigator (using Spotlight) and run Spyder from there. Another option is to open Terminal.app and run spyder or spyder3 there too.
You don't need to have our source code at all to use Spyder because it is installed by Anaconda or pip.
Finally, I don't know what Python version you used to run pip3 install -U spyder, but you should really remove what you installed with it and use Anaconda instead. As we said in our installation instructions too:
We also support pip, but please be aware that pip installations are for advanced users with good knowledge of all Spyder dependencies.
This clearly seems to be not your case.

Categories

Resources