Python Can't Find Module After Installing with Pip and Conda - python

I am on Windows Subsystem for Linux (WSL). I've defined an environment and I am trying to add the palettable package to it. This is what I tried:
conda install palettable, went fine no errors, tried to do import palettable in my script and I get the error ModuleNotFoundError: No module named 'palettable'
Next I did conda remove palettable
Then I installed again, this time using pip by doing pip install palettable
I get the same error
Did I miss a step? Or do something wrong?
I've added many other packages to this same environment using conda and not had any problems or encountered this error before.

You can easily fix this problem by restarting the kernel or IDE in which you are writing the code after installing the module..

I found the problem. I am using VSCode and I didn't realize the python interpreter and the notebook kernel are set independently. The interpreter correctly reflected that I was using my project environment. But the notebook kernel (top right corner of NB window) was not set to the same thing. Once I set it correctly and restarted the IDE it now is correctly finding the packages.

Related

Why can I not import a module (Batman) in Jupyter Notebook

I'm trying to import a module (batman, for model transits) in jupyter notebook, but its telling me file not found. However when I use the anaconda terminal it imports without issues
This is the error I get from Jupyter Notebook
I've tried reinstalling the package, and I've tried to find a kernel in which it works, but I'm not sure how to go about that
any help is appreciated
Try checking if the module installed properly by running this:
python -c 'import batman; batman.test()'
the batman module also seems to have some issues when installing on windows.
Read this github if you're on windows https://github.com/lkreidberg/batman/issues/26
I've faced the same issue when I use Jupyter in an Anaconda environment. Jupyter Notebook doesn't use the same kernel as Anaconda and I had to specify it manually.
What you can do is comparing the Python version on your Anaconda environment by running this command in your shell:
python --version
And in your Notebook :
import sys
print(sys.version)
Are the versions different ? If so, you must add your Anaconda kernel to your Notebook :
ipython kernel install --name "local-anaconda-env" --user
Then select it in your Notebook.
I was facing the same issue in VSC. What I did is that first I moved the folder(In your case batman) to where VSC had been installed on my laptop. Then I changed the default directory to where VSC was installed on my laptop by running the following code in VSC.
import os
os.chdir("The path where VSC is installed")
Also If you could try to do the code in a python file instead of jupyter notebook
In a cell within your notebook run the following:
%pip install batman-package
Based on here.
And the use of modern pip install magic to handle installing to correct enviornment from inside a notebook, see here.
For others, hitting here and not seeing what they want/expect.
Above is for batman:
Bad-Ass Transit Model cAlculatioN. Associated Github Repo is here, and Python Package Index page.
This is a poor name for a package as at least three developers have claimed this. Also see:
Anaconda install for Batman at conda-forgeBayesian Analysis Tool for Modelling and uncertAinty quaNtification
BATMAN: A Python deployment toolbelt -Batman 0.7.0 at the Python Package Index
Maybe those links will help you.

Removing one of the two installed Jupyters

In my system, by mistake, a second Jupyter is now installed.
which -a jupyter:
/opt/homebrew/bin/jupyter #latest jupyter which should be removed
/opt/anaconda3/bin/jupyter #previous working jupyter
While the second one,"/opt/anaconda3/bin/jupyter" is what I was working with, the first one,"/opt/homebrew/bin/jupyter" does not work because of its wrong python path and uninstalled packages.
I have tried to remove this jupyter by brew uninstall jupyter, but it didn't work because of this error Error: No such keg: /opt/homebrew/Cellar/jupyter.
As I have numerous packages and libraries on the second jupyter.
Thus, I am not willing to reinstall jupyter from scratch.
Calling Jupyer notebook at the moment calls the first path. Do you have any suggestions to fix this issue?
You have a python that was installed through brew, you should be able to do
/opt/homebrew/bin/pip3 uninstall jupyter
Note that the underlying issue seems to be that your PATH is not set such that the anaconda bin folder takes precedence over the homebrew/bin folder, which you might or might not want to adress. You could also consider uninstalling the homebrew python version with
brew uninstall python

Spyder can't find installed modules with pip

I first installed Spyder and then afterwards Python on a server (with Windows Server 2019) all on the directory "C:\Users\wi932\ .spyder-py3" and the Python folder "C:\Users\wi932\Python\Python38". Then I installed many packages (like tensorflow, matplotlib, scikitlearn) by using the command prompt of windows and pip from the directory "C:\Users\wi932\Python\Python38\Scripts" and it was okay.
However, when running Spyder I can't use the packages that I installed using pip. Whenever I run a Python programm with those modules I get an error message "ModuleNotFoundError: No module named 'tensorflow'". So the question is how can I build a "connection" between the pip and Spyder?
Does anyone have an idea? I'll appreciate every comment.
Have you considered installing anaconda?
Spyder comes as a part of this and is easy to install from the anaconda client. You can also create your base environment in the anaconda client and then switch to pip to install packages from there.

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

Importing Numpy results in error even though Anaconda says it's installed?

I signed up for a statistics udemy course which uses jupyter running the stock numpy package out of anaconda.
Numpy is working when I run python 3.4.4 in pycharm, but it will not work in either anaconda2 nor anaconda3.. this makes no sense because numpy comes stock as part of the anaconda library.
When I try importing numpy in jupyter, running a local instance of a python 2 script, I get this:
Thinking I could sidestep the error using a IDE, I tried pycharm and I got this:
Numpy is part of the anaconda default library which I'm running, so I checked and made sure the numpy package was there via the Anaconda Prompt using 'conda list'...
Why won't it import successfully?
Before I uninstall and reinstall everything, does anyone have any ideas?
Yayyyy. I figured it out. So I had several different python versions before I was running python through anaconda/jupyter. As a result, the tethering in-between the additional package libraries to the version of python running was shifty. For example, I may have had numpy working configured to 3.4 but not 2.7..
anyways, to break down the steps I took, I uninstalled all instances of python (both anaconda and normal versions). Then I deleted the old 3.4 libraries from my C drive. Then I installed anaconda 2.7.11 again, tested importing numpy and got the same error. Then I thought to myself, what if the packages are installed separate from the python library and their configuration didn't get reset via the reinstall.. so via the anaconda prompt I typed:
pip uninstall numpy
which removed the package I was having trouble with. and then :
pip install numpy
which downloaded and reset a whole new instance of the package. Then I tested importing numpy in both the anaconda prompt and jupyter = both worked.
I'm very happy that this ended up working out as I can continue on as planned. For anyone else who experiences a similar problem, I would try uninstalling, then reinstalling the problem causing package via pip commands - this step seemed to have the most impact on fixing the problem. Then if that doesn't work proceed to uninstall and reinstall the environments in intervals.
You can try using the following command:
pip install numpy --upgrade
This will uninstall old installed version of numpy and install a new version. This command solved my issue.
try activating you base conda environment before starting jupyter.
>activate root
>jupyter notebook

Categories

Resources