No module named scipy even though it is installed - python

I am trying to use scipy but when I import it with import scipy, it says "no module named scipy. I have used pip list to see if it is installed and it is, and i have tried pip install scipy but it just said that it was already installed. Does anyone know how to fix this?
SOLVED: ran the code with python3 instead of python

it is possible you have 2 versions of python installed, python2.7 or python3.x .. when you install stuff with pip and want to use it for a specific python version:
pip install scipy is for python 2.7, so its "python" in the terminal and "python x" to run scripts
pip3 install scipy is for python 3, so its "python3" in the terminal and "python3 x" to run scripts
in your case, you would be calling scripts like:
python pythonFile.py
you would be installing packages like:
pip install package
and you would be using python in the terminal with "python"
if that still doesn't work, for me solving that problem was updating numpy. not sure if it would be the same with you. hope this helped

Related

python module i installed with pip shows up on pip list, but i cant import it into code?

others have asked this question but the answers are either too hard for me to understand or dont help.
i tried using many variations of python3 and python, pip3 and pip, sudo etc. they all said the package was already present. pip list command shows that numpy v1.23.1 is installed.
I tried uninstalling and reinstalling but that didn't work either.
for context I'm tryna install numpy, python version:3.10.5
It might be that your Python and your pip module do not belong to the same environment/installation.
To make sure you install a module under the Python installation you want to use, run this at a command prompt:
python -m pip install numpy

mac pre-bundled python and newly-installed one

I'm trying to install python and its library in bash, using brew and pip.
When I type command
pip list
it seems that numpy(1.8.0rc1), matplotlib(1.3.1), scipy(0.13.0b1) are all installed. However, when I type ipython command and enter the interactive python interface,
import numpy
works fine, but
import matplotlib
import scipy
run into error saying that "ModuleNotFoundError".
I think it is because OS has its own pre-bundled python and pip list command shows what libraries are installed for the pre-bundled one. But ipython command enters into the newly-installed python where those two libraries are not installed.
So could any one talk about the two pythons, and how could I install the library to the correct position and enter the proper python.
I've tried brew, reinstall, pip, sudo and they didn't work quite well.
BTW, when type print(sys.path) in ipython, it gives
['', '/usr/local/Cellar/ipython/6.2.1/libexec/bin', '/usr/local/Cellar/ipython/6.2.1/libexec/lib/python3.6/site-packages', '/usr/local/Cellar/ipython/6.2.1/libexec/vendor/lib/python3.6/site-packages', '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages', '/usr/local/Cellar/numpy/1.13.3/libexec/nose/lib/python3.6/site-packages', '/usr/local/Cellar/ipython/6.2.1/libexec/lib/python3.6/site-packages/IPython/extensions', '/Users/bazinga/.ipython']
The reason behind this is that you now have two Python installations, one being the system one:
$ python -V
Python 2.7.13
and the other one installed via Homebrew ("brewed" Python):
$ python3 -V
Python 3.6.3
When you issue pip list, you are listing packages installed for the system Python. You can check what Python installation does pip belong by issuing pip -V. The package manager for the brewed Python is pip3, check this: pip3 -V. All the commands valid for pip will also work with pip3, for example list packages by issuing pip3 list etc.
The ipython Installation uses the brewed Python, so in order to install packages so be accessible by ipython, use pip3:
$ pip3 install --user numpy matplotlib scipy

Installing Python Libraries When Multiple Versions of Python Exist

When I run python -V from terminal, I see that Python 2.7.10 is installed. I want to keep this as the "global" version as OSX utilizes it.
When I run Idle, I see that Python 3.6.0 is running. How do I install libraries to this version of Python?
For example, if I run pip install bs4, the library is installed here beautifulsoup4 in /Library/Python/2.7/site-packages/beautifulsoup4-4.5.3-py2.7.egg - which is obviously Python 2.7.
So when I run my script from Idle, I get the following error:
ModuleNotFoundError: No module named 'bs4'
You want to use virtualenv. It sounds like you have two versions of Python installed and you need to focus on one while being able to manage the packages in each. Virtualenv will do this for you.
First install virtualenv (https://virtualenv.pypa.io/en/stable/),
Second run it specifying the version of python you want as so: `virtualenv -p /usr/bin/python2.6
Third you can use pip to install packages directly into this environment. This increases the amount of disk space you need, but will allow you greater control over your code.
When you have two versions of python, you will need to specify which version of python you would like to run. You can do this with the activate command. For example:
activate python3
Once you have activated the python 3 environment, you can then run pip:
pip3 install bs4
which will install the beautiful soup library in your python 3 environment.
Another answer was found here: https://stackoverflow.com/a/4910393/1580659
pipVERSIONNUMBER install will install the library to the correct version of Python.
$ pip2.6 install otherpackage
$ pip2.7 install mybarpackage

ImportError: undefined symbol: _PyUnicodeUCS4_IsWhitespace

I am a python beginner and I would like some help with this. I am using Ubuntu and I had installed python using Anaconda, but then I tried to install it again using pip and now when I'm trying to run my code, at import numpy as np, I see this error
ImportError: /home/dev/.local/lib/python2.7/site-packages/numpy/core/multiarray.so: undefined symbol: _PyUnicodeUCS4_IsWhitespace
How can I fix this?
I also got this error. If you google for it, you will find lot's of similar issues. The problem can happen when you have multiple Python versions. In my case, I had the Ubuntu 16.04 Python 2.7 via /usr/bin/python and another Python 2.7 via Linuxbrew. type python gave me /u/zeyer/.linuxbrew/bin/python2, i.e. the Linuxbrew one. type pip2.7 gave me /u/zeyer/.local/bin/pip2.7, and looking into that file, it had the shebang #!/usr/bin/python, i.e. it was using the Ubuntu Python.
So, there are various solutions. You could just edit the pip2.7 file and change the shebang to #!/usr/bin/env python2.7. Or reinstall pip in some way.
In my case, I found that the Python 2.7 via Linuxbrew was incompatible to a few packages I needed (e.g. Tensorflow), so I unlinked it and use only the Ubuntu 16.04 Python 2.7 now.
Just uninstall numpy:
pip uninstall numpy
And reinstall numpy:
pip install numpy
Another thing you can do is run it on a virtual environment:
virtualenv myproject
cd myproject
source bin/activate
pip install numpy

Installing OpenMDAO

I am trying to install OpenMDAO, which is an open source framework that uses python. So before I get to it, it needs 3 packages:
Fortran Compiler
NumPY
ScyPy
Which I did install using homebrew:
brew install gfortran
sudo easy_install-2.6 pip
sudo pip-2.6 install numpy
sudo pip-2.6 install scipy
Now, to install OpenMDAO, I'm supposed to be in the OpenMDAO folder and at that level I should run this script:
python go-openmdao-dev.py
But I keep getting this error:
ERROR: the following prerequisites could not be imported: ['scipy'].
So I was talking to the people at OpenMDAO, and it seems that I need to use is python2.6 - although I'm not certain, so my guess is that all the packages I installed are for python 2.7 which I downloaded since it was recommended in the python website.
The one that comes with Mac was not. So when I type in terminal the following:
python2.6
It actually runs that version. So at this point I don't know if I have to go run the script in python 2.6, but if so, I have no idea how to do it. I tried doing this and it did not work:
If anyone is familiar with the environment or just with python itself and has any suggestions, I'll really appreciate it.
This question was answered on the OpenMDAO support forum:
http://openmdao.org/forum/questions/744/error-the-following-prerequisites-could-not-be-imported-scipy
The solution was to install scipy and numpy for python2.7. The original poster first installed them for python2.6, but then ran the go-openmdao.py script with python2.7. Once all the pre-reqs were installed for 2.7, it worked.
I had the same problem after downloading scipy from a .dmg. I uninstalled, and then reinstalled from source: http://sourceforge.net/projects/scipy/files/scipy/0.12.0/ and then the openmdao installer recognized scipy.
This may be useful (from INSTALL.txt in scipy-0.12.0.tar.gz):
To test SciPy after installation (highly recommended), execute in
Python
>>> import scipy
>>> scipy.test()
To run the full test suite use
>>> scipy.test('full')
Please note that you must have version 0.10 or later of the 'nose'
test framework installed in order to run the tests. More information
about nose is available on the website__.
__ http://somethingaboutorange.com/mrl/projects/nose/

Categories

Resources