numpy does not work through terminal - python

dI have installe numpy on ubuntu by executing
sudo apt-get install python-numpy
while executing on terminal I get this error.
>>> import numpy as np
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>>
why this is happening? I tried many a times by going through internet but I couldnt find a solution.Could you please tell me how to solve this?
Edit1:
I came to know that I have to install numpy for the python version which I run on terminal, using pip.. Python 2.7.3 runs when I enter 'python' on terminal. So that means I have to install numpy for python 2.7.3. Can someone guide me how to do it? I couldnt figure it out by myself. BTW I am using Ubuntu 12.04 if that helps.
Edit 2:
I did some more digging into this.. my /usr/lib contains two directories python2.7 and python3.While Python2.7 directory consists of a large number of files and sub directories,python3 directory has only dist-packages subdirectory which consists of deb_conf.py anf lsb_release.py..I think I tried python3 few months back and then removed it..But right now python2.7 is the only thing i am having.
Edit 3:
SO here are the outputs of the commands you asked me to enter
~$ virtualenv --no-site-package -p /usr/bin/python2.7 my_env
Running virtualenv with interpreter /usr/bin/python2.7
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in my_env/bin/python2.7
Not overwriting existing python script my_env/bin/python (you must use my_env/bin/python2.7)
Installing distribute..............................................................................................................................................................................................done.
~$ source my_env/bin/activate
~$ pip install numpy
last command gave a generated a lot of logs which ended with something like this..
Creating build/scripts.linux-i686-2.7/f2py2.7
adding 'build/scripts.linux-i686-2.7/f2py2.7' to scripts
changing mode of build/scripts.linux-i686-2.7/f2py2.7 from 664 to 775
changing mode of /home/sandeep/my_env/bin/f2py2.7 to 775
Successfully installed numpy
Cleaning up...
After all these I tried to run python again and this is the output.
~$ python
Python 2.7.3 (default, Jan 20 2013, 21:40:19)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>>

In case nothing works.
Install python-virtualenv if it's not yet done.
Create a virtual env
virtualenv name
Start the virtualenv
source name/bin/activate
Install numpy with easy_install or pip
Profit
Note:
Virtualenv activation has to be done everytime. But you can make that task easier with virtualenv wrapper.
http://virtualenvwrapper.readthedocs.org/en/latest/
There are a lot of reasons to use virtualenv instead of ubuntu packages. In some way, I recommend not touching as much as possible the "OS" python. And if you need it for a project, use virtualenv. Python in virtualenv won't mess with other apps and you don't have to use sudo to install new packages.

Related

Numpy import error even after installing it with current python version

Note - I already check numpy import error related threads but none helped
I am using debian 8 where default python is 2.7.9. I installed python 3.4.2 and created virutal env.
Within virtual environment -
python -V
Python 3.4.2
pip -V
pip 1.5.6 from /path/venv34/lib/python3.4/site-packages (python 3.4)
I have python3 numpy package - python3-numpy_1.12.0-2~pn0_amd64.deb
which I have installed with sudo dpkg -i python3-numpy_1.12.0-2~pn0_amd64.deb
which successfully completed.
Now when I do
python
Python 3.4.2 (default, Feb 7 2019, 06:08:06)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'numpy'
>>>
Any clue what's wrong here?
python3.4 -m pip install numpy==1.12.0-2
ok since my repo is less than 50 i can not add comments, so take this answer as a comment to your question.
I think numpy is installed but not in your virtualenv, make sure your virtualenv is active when you are trying to install any library, you will see virtualenv name in every command line if it is activated.
(venv) C:\Users\seventeen\sprint25>
Try python -m pip install numpy==1.12.0. This should help you.

psycopg2 module cannot be found by Python2.7

I installed psycopg2 via pip, but my programs are having trouble finding it.
So, I tried to install psycopg2 via pip again:
user#ubuntu:~/Desktop/progFolder$ sudo pip install psycopg2
Requirement already satisfied (use --upgrade to upgrade): psycopg2 in /usr/local/lib/python2.7/dist-packages
Cleaning up...
Then I tried to use a program that imports it:
user#ubuntu:~/Desktop/progFolder$ python myProg.py
Traceback (most recent call last):
File "myProg.py", line 6, in <module>
import psycopg2
ImportError: No module named psycopg2
And I have tried just importing directly in python:
user#ubuntu:~/Desktop/progFolder$ python
Python 2.7.5 (default, Nov 9 2014, 14:14:12)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named psycopg2
So I printed my python path.
>>> import sys
>>> print sys.path
['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages']
And noticed that the path does contain the path to psycopg2.
psycopg2 in /usr/local/lib/python2.7/dist-packages
So, I have no idea on why this is happening. Any help would be appreciated.
UPDATE:
I have done
>>>help()
>>>modules
And psycopg2 was not listed among the other modules. (this does not help me but may help you help me)
Your pip looks ok (that is, it's the system/default one). Your Python executable, however, is something that didn't come by default with 14.04 LTS (e.g., on my 14.04 system, it's /usr/bin/python). Did you install that Python yourself? Then you need to install (and use) the corresponding pip as well. (Normally, Python would have come with a pip installation, but apparently in this case, it didn't.)
pip can be fairly simple installed from its installation instructions.
Though first, verify that
you did install /usr/local/bin/python yourself. That is, it didn't come with some other piece of software that you installed and that, along the way, decided to install Python there.
you want to use /usr/local/bin/python (I guess it is a more recent version of Python 2.7; the default 14.04 LTS one appears to be 2.6.7 as of 2015-08-03).
From your python path print, it looks like it doesn't have /usr/local/lib/python2.7/dist-packages included in it. You can add it in one way by:
sys.path.insert(0, "/usr/local/lib/python2.7/dist-packages")

Using Python with homebrew on OS X

I have been using Mac python for a while, and I decided to teach myself matplotlib, because I want to have the experience with some common modules. I hear from everyone that once you get into non-standard modules, it's best to use python threw homebrew, so you have access to pip and not easy_install. After running: $brew install python --with-brewed-openssl, $brew install python3 --with-brewed-openssl, and $pip install matplotlib, I go to the python shell with $python3. Once there, I run import matplotlib.pyplot as plt, and get the following:
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'matplotlib'
>>>
I have a feeling I'm still using the mac python, and not the brewed python, and I have tried editing the path, but it isn't working, even though I am running $source ~/.bash_profile after every edit. Just in case I am editing the path wrong, I will post the file contents below:
PATH=/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
PATH=/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
export MSF_DATABASE_CONFIG=/usr/local/share/metasploit-framework/database.yml
PATH="/Users/ericmarkmartin/.apportable/SDK/bin:$PATH"
PATH="/Users/ericmarkmartin/.apportable/SDK/bin:$PATH"
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
PATH=“/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/bin:${PATH}”
export PATH
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin:/Users/ericmarkmartin/.rvm/bin
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
# Setting PATH for MacPython 2.6
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}"
export PATH
export PATH=/usr/local/bin:/usr/local/sbin:${PATH}
$pip3 install matplotlib returns the following
RuntimeError: Broken toolchain: cannot link a simple C program
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /private/var/folders/sr/j0s763cj06v38c6btg6v7k7r0000gn/T/pip_build_ericmarkmartin/matplotlib
Storing debug log for failure in /Users/ericmarkmartin/.pip/pip.log
If more information is needed, please feel free to ask, and thank you so much in advance for the help!
pip usually installs modules for python2 if you have both python versions in your system, so for python3 you will probably need to use pip3.
For reference:
https://docs.python.org/3.4/installing/index.html
You can check where a command is coming from by using which python . In this case you are adding /opt/local/bin/ to your path, but then you are adding /Library/Frameworks/Python.framework/Versions/2.6/bin and /usr/local/bin/ before them (you are prepending in each case). $PATH stops on the first successful match, so you won't pick up the python from /opt/local/bin . Try moving that line to the end of your bashrc or adding another line prepending it to the front of $PATH.

How to install numpy in a virtualenv on Debian?

(NB: see this other post for why I am not using dpkg/apt-get/etc. for this installation.)
I can install numpy in a virtualenv on Debian with, e.g., pip:
(base)[1778]% pip -v install numpy
Downloading/unpacking numpy
...
<output omitted>
...
Successfully installed numpy
Cleaning up...
Removing temporary dir /home/jones/.virtualenvs/base/build...
But immediately after this:
(base)[1779]% python
Python 2.7.1 (r271:86832, Jun 22 2011, 15:39:05)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>> ^D
Any ideas?
OK, I found the problem. It turns out that, even though my virtualenv is active (see the (base) prefix to the command-line prompts in the screen interaction snippets above), I still need to tell pip to use it. E.g. after running something like
pip -E /path/to/virtualenv install numpy
then importing numpy within an interactive python session succeeds (whether the imported module is functional, I don't know yet).
This is absurd: my virtualenv is active, and the pip executable I'm running is the one installed in that virtualenv:
(base)[1801] which pip
/home/jones/.virtualenvs/base/bin/pip
So what's the point of having a virtualenv if pip will not use it by default???
I'm guessing that your virtualenv is not actually active?
You might also run into problems with this bug: https://bugs.launchpad.net/ubuntu/+source/python-virtualenv/+bug/780220
There is a similar question here: Windows + virtualenv + pip + NumPy (problems when installing NumPy)
perhaps some of the answers there may help you.
regarding the last error for command:
pip -E /path/to/virtualenv install numpy
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Here is solution to that problem.
Upgrade to latest virtualenv:
sudo pip install --upgrade virtualenv
create your python virtualenv and run
pip -E /path/to/virtualenv install numpy
Regards, Karlo.

opencv python osx

(Step 1)
I'm trying to get openCV to run from python on my mac using the MacPorts install http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port, and also trying to follow The Petite Geek's guide:
sudo port -v install opencv +python26
It runs for about 10 minutes without errors.
(Step 2)
I download ctypes-opencv source and demo files. I navigate to the src directory and run:
sudo python setup.py install
I see like 50 lines almost all of the form: copying ... -> ..., which looks good to me. No errors here.
(Step 3)
I add export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib to the end of my ~/.profile.
(Step 4)
I open a new terminal to test my install. From my home folder:
$ python
Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cv
>>>
Does not work.
I read somewhere that opencv installs python bindings with the default version of python for OSX, and I am probably running a non-default version, but this is not actionable information to me.
I struggled for a while with Python on Mac. Here is how I finally (and easily!) installed it. Remove all the things Python you have on there already. They will probably be located at /Library/Frameworks/Python.Framework and /opt/local/var/macports/software/py26*
First download Python with Macports.
sudo port install python27
Then make sure your system is using this version with:
sudo port select --set python python27
Next install numpy with:
sudo port install py27-numpy
Now install opencv:
sudo port install opencv +python27
Now edit your ~/.bash_profile with:
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit ~/.bash_profile
or
open -t ~/.bash_profile
and add the line:
export PYTHONPATH=/opt/local/var/macports/software/opencv/2.2.0_0+python27/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:$PYTHONPATH
or where ever your version of the cv.so file is hidden....
Now restart terminal and try:
%python
>>>import cv
I'm using Netbeans for opencv and python and it works really nice. Good luck.
$ brew search opencv
homebrew/science/opencv
$ brew install homebrew/science/opencv
after installed, there is warning:
==> Caveats
If you need Python to find the installed site-packages:
mkdir -p ~/Library/Python/2.7/lib/python/site-packages
echo '/usr/local/lib/python2.7/site-packages' > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth
so, just do
mkdir -p ~/Library/Python/2.7/lib/python/site-packages
echo '/usr/local/lib/python2.7/site-packages' > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth
If you notice the first line output when running python, you'll see that you're still using the Apple-supplied Python interpreter. Try installing and using the python-select package in MacPorts and then try the instructions again starting from step 2.
Also make sure you followed all of the steps when installing MacPorts so that /usr/local/bin is on $PATH.
Another "hack" I found during my struggles using CMake (but maybe the problem is the same with ports) : it appears that the python modules location has been duplicated on my Mac OS Lion, for a reason I can't explain.
CMake wants to put the "cv" module here :
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Whereas my default Python interpreter is looking here (thanks PyCharm for telling me) :
/Library/Python/2.7/site-packages
Moving both cv2.so and cv.py files to the second location, did the trick for me. I don't know if this is the cleanest way.
Hope it can help some googlers !

Categories

Resources