Python modules installed, but won't import - python

I recently upgraded to OSX Sierra, and now I can't import any packages in Python. When I run a standard import numpy in the python console (I'm using Pycharm) I get
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: No module named numpy
I think the problem's coming from when I reinstalled my ports when I was migrating macports, according to the instructions here. I'm pretty sure everything was working in python after I'd upgraded to Sierra, but broke once I did this.
My first thought was to reinstall numpy, but when I go to the command line and run pip install numpy, I get
Requirement already satisfied: numpy in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python.
However, running which numpy turns up nothing, which seems weird, since it just told me that numpy's installed, and I can find it on my computer if I use easyfind. Since it might be relevant, which python gives me /opt/local/bin/python.
I've tried a few different fixes, but haven't been able to get anything to work so far. I have a feeling that I'm missing something very basic, but can't figure out what it is. I'm running python 2.7.12 on mac 10.12.1.

In my PyCharm I use virtual environments. An upgrade to Sierra may have, as you suggested, removed the Python port and thus your virtual environment may need to be recreated too.

Not exactly solving the original problem, but I did figure out how to work around it. I just installed python 3.3 (via macports) and set it to be the default python of my computer, and then reinstalled Pycharm (although I probably could have just restored settings to their defaults). Everything seems to be working now, in python 3.3, although I do have to use pip as python3.3 -m pip install rather than the standard pip install.

Related

How do I update python on my Raspberry Pi to at least 3.6?

As some brief background information: I was origianlly trying to use Miniconda (with conda) to install dependencies that I need for my project on my Raspberry Pi. After trying to use Conda to install the SimpleAudio package, I got an error saying that it did not exist, therefore I proceeded to install this through Pip. Pip found the correct package although I get the following error message:
pi#raspberrypi:~ $ pip install simpleaudio
Traceback (most recent call last):
File "/home/pi/miniconda3/bin/pip", line 7, in <module>
from pip._internal.cli.main import main
File "/home/pi/miniconda3/lib/python3.4/site-packages/pip/_internal/cli/main.py", line 10, in <module>
from pip._internal.cli.autocompletion import autocomplete
File "/home/pi/miniconda3/lib/python3.4/site-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
from pip._internal.cli.main_parser import create_main_parser
File "/home/pi/miniconda3/lib/python3.4/site-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
from pip._internal.cli import cmdoptions
File "/home/pi/miniconda3/lib/python3.4/site-packages/pip/_internal/cli/cmdoptions.py", line 28, in <module>
from pip._internal.models.target_python import TargetPython
File "/home/pi/miniconda3/lib/python3.4/site-packages/pip/_internal/models/target_python.py", line 4, in <module>
from pip._internal.utils.misc import normalize_version_info
File "/home/pi/miniconda3/lib/python3.4/site-packages/pip/_internal/utils/misc.py", line 20, in <module>
from pip._vendor import pkg_resources
File "/home/pi/miniconda3/lib/python3.4/site-packages/pip/_vendor/pkg_resources/__init__.py", line 92, in <module>
raise RuntimeError("Python 3.5 or later is required")
RuntimeError: Python 3.5 or later is required
It seems I need to update Python, although when I print the verion on Spyder IDE, it says I am already using 3.7.
Have I caused some sort of mismatch between what version my IDE is using and what the default process the terminal uses to look up the version? I noticed that it is looking for the Python version under Miniconda. If I can update If so is there a fix for this?
Please as me for more information if required (I am fairly new to stack overflow).
UPDATE:
I have been able to install the updated version of Python to 3.6 using the following instructions:
https://stackoverflow.com/a/56852714/12361146
This generally solves the scope of this question in terms of how I update Python, but I am still confused as to why Spyder IDE uses a more up-to-date version of Python whereas the terminal shows otherwise.
To answer the question of why Spyder reports a more up-to-date version of Python, here's the reason. The default versions of Python that are installed with Raspbian are 2.7 and 3.5, located in the /usr/bin/ directory. When you install Spyder, however (either independently, or more commonly, using conda), it includes its own installation of Python, which it is configured to use in the IDE, and which is located in a different directory. Hence when you compare the versions, first by entering python3 --version on the command line, and then print(sys.executable) from the Spyder IDE, they're different.
Now the issue with using pip alongside conda for updating the Spyder installation of Python is that it has the potential to mess it up quite badly, so avoid that unless you really know what you're doing. From code you posted above, you have avoided that, though, since that will have impacted the default Raspbian installation of Python, not the Spyder one. Upgrading the latter version should be done using Conda, not pip.
Hopefully you're now all up and running.
You can install newer versions of python using the package manager apt or apt-get.
Start by getting up-to-date package definitions.
$ sudo apt-get update
Then you can show details about the python3 package.
$ apt-cache show python3
When I run that now I get "Version: 3.7.3-1".
To install the python3 package and all its dependencies.
$ sudo apt-get install python3
You will still need to type python3 and pip3 when you run the commands because you are not replacing the built-in python 2.7.
Try these commands to see what you get
$ python --version
$ python3 --version
If you want to change the default python to python3 then have a look at this answer How to change the default python version in Raspberry Pi

Installing and importing Python Modules onto OS X

I've been trying for a couple of hours already. It seems IDLE can't find any third-party module. I am a Python beginner.
Here is some info about my system:
OSX version: 10.11.5
python version: Python 2.7, Python 3.4, Python 3.5
The initial installation using pip (among other methods) seems to work fine. When I repeat the installation, terminal responds with:
Requirement already satisfied (use --upgrade to upgrade): pyperclip in
./anaconda/lib/python3.4/site-packages
However, when I go to IDLE (Python 3.4) and try to import the module, IDLE responds with:
Traceback (most recent call last): File "", line 1, in
import pyperclip ImportError: No module named 'pyperclip'
I have read that it may have something to do with my PATH or some virtual environment. I’ll be frank, I’m not sure what to make of these as they seem beyond my current ability.
This inability to import modules is becoming an almost insurmountable roadblock to advancing with Python. If you can offer any ideas on what I can do or can ELI5 the solution, I am forever in your debt?
It seems you are using conda, but you are trying to install the pyperclip module with pip. Have you tried running conda install pyperclip?
As stated here:
Because Conda introduces a new packaging format, you cannot use pip and Conda interchangeably; pip cannot install the Conda package format. You can use the two tools side by side but they do not interoperate either.

checking virtual environment modules

So I am following this tutorial and I am about to give up since i am working on a windows machine and I am pulling my hair out finding equivalencies.
I have a virtual environment, but when I try to run my code it dosent appear to be finding the local dependancies!!
(venv) PS C:\Users\Vince\skellys\Flask_skelly\venv> python Bartender_no_ui.py
Traceback (most recent call last): File "Bartender_no_ui.py", line 7, in <module>
import lxml
ImportError: No module named lxml
How can i check if the dependencies are there and how do i make sure my code is running from venv?? Am I just to much of a noob to be trying this tutorial anyway??
maybe relevant details- I had to use Conda rather than pip to install lxml, because pip couldnt get the dependencies of libxml2 and libxslt, but I get no module error for the other modules that I installed in the venv using pip as well
If you do
pip freeze
That should show you what modules you have installed in your virtualenv as long as you have it active.
Seeing as how you are working on windows I'd check out this site Its what I followed when I first got started.
Haven't used Conda much myself so not familiar with how it works but I'm guessing that won't install to your virtualenv. If you download lxml from here you should be able to install it into your virtualenv on windows.
its not the best solution but I figured out a hack around - I copied lxml from my c:\anaconda\pkgs folder. Code appears to be running fine. figures.

no module named 'numpy' (windows 7, python 3.4.3, numpy-1.9.2)

Please only respond to this post if you use Python on Windows, rather than Mac or Linux.
Error message:
>>> import numpy
Traceback (most recent call last):
File "stdin", line 1, in <module>
ImportError: No module named 'numpy'
Questions:
Regarding windows 7, python 3.4.3 and numpy-1.9.2, are there any
conflicts that would prevent these from working together?
Into which directory (please write out the complete directory path
starting with "C:") should I extract the contents of the numpy zip
file?
What is the exact command that I need to type into python command
prompt in order to install numpy?
Thanks in advance for your assistance
I was also facing this issue where in I tried using
import numpy
But it has given me error "ImportError: No module named 'numpy'"
I installed numpy using "C:\user>python -mpip install numpy" and it was successfully installed.
However I was again getting the same error
Then I checked that the path where in numpy was intalled was not listed in
import sys
print(sys.path)
Then I appended my path wherein numpy was installed using ">>> sys.path.append(r"C:\Users\xxxx\AppData\Roaming\Python\Python38\site-packages").
The above command worked fine, still problem not resolved then I restarted my python session again.
Finally it worked..!!!!(As after every append we have to restart our python session).
1) No there isn't.
2) You can use pip. pip install numpy
If you don't have pip installed, install it, its the most often used way of installing python packages. Yes it is possible to do under windows.
3) Once you have the python command prompt, you already have python installed.
1) no
2) It doesn't actually matter as pip or the installer will figure that out for you. However, just so you know, pip or whatever will install numpy into your site-packages folder.
3) I recommend downloading the NumPy installer from SourceForge:
http://sourceforge.net/projects/numpy/files/NumPy/1.9.2/
Then you don't have to worry about having the right compiler installed. However, if you want to use pip, then you'll need to install the appropriate compiler. This is documented at the following locations:
Microsoft Visual C++ Compiler for Python 3.4
https://matthew-brett.github.io/pydagogue/python_msvc.html
I'm not even close to an expert on Python. That said, I like the notion of "keep it simple." I chose to install a Python distribution that already includes numpy. Specifially I installed Python(x,y). Everything seems to be working just fine right after install. I appreciate all those who commented on my question. Thanks

Error when running tests of scipy and numpy on OS X Snow Leopard

I am new to stackoverflow as well as Python, and I hope to use stackoverflow to learn and improve my Python programming.
However, as soon as I set up Python, SciPy, NumPy on my Mac, I encountered a problem when I tried running a full test of SciPy and NumPy to verify the install:
>>> import scipy
>>> scipy.test()
Running unit tests for scipy
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
scipy.test()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/nosetester.py", line 318, in test
self._show_system_info()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/nosetester.py", line 187, in _show_system_info
nose = import_nose()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/nosetester.py", line 69, in import_nose
raise ImportError(msg)
ImportError: Need nose >= 0.10.0 for tests - see http://somethingaboutorange.com/mrl/projects/nose
As suggested by the error message, I install this 'nose' package via easy_install, and the install seems successful as I get the following from bash:
mlstr:~ mlstr$ nosetests -V
nosetests version 1.1.2
So I tried the SciPy test again, and it failed with the same message. Can someone please let me know if I need to do anything else to install/configure nose so that it can be used by the unit tests of SciPy and NumPy. Thanks in advance.
UPDATE #1: After trying Rahul's suggestion of using pip to reinstall nose, I think I've found where the problem is: whenever I do easy_install or pip from bash, the package is installed into Python 2.6 directories instead of 2.7 ones. And I think this is because by default 2.6 is the one shipped with Snow Leopard. So what should I do to tell easy_install or pip that I want to install a particular package for Python 2.7? Thanks.
UPDATE #2: By following up my update above, I've found the solution here: easy_install with various versions of python installed, mac osx. It is indeed because that I used the wrong version of easy_install thus my nose installation was for Python 2.6 shipped with Mac OS X. Thanks.
I tried replicating the error that you were encountering. I did not have nose, so I was getting the same error.
I installed nose using pip
sudo pip install nose
After that the scipy.test() worked. I did nothing else. Did you install nose using pip? If not, try using pip.
My machine has :
SciPy version 0.10.0.dev
Nose - 1.1.2
If you want to use your locally installed python version you need to make sure, that this is called when you run python from console.
On a linux machine an alias would do that. Once you run the correct python from your console all the easy_install things will be available in the correct python version.

Categories