I have installed python on my macbook with the python installer from Python.org. Subsequently, I went and installed pip, ipython, and numpy. Everything seemed fine. However, now I am getting the following problem. I can import numpy when I run ipython, but not when I run regular python.
E.g.
Logister-MacBook-Pro:~ Logister$ ipython
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
Type "copyright", "credits" or "license" for more information.
IPython 3.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import numpy
In [2]: import site; site.getsitepackages()
Out[2]:
['/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/Library/Python/2.7/site-packages']
However, when I try to do the same thing in python 2.7.9:
Logister-MacBook-Pro:~ Logister$ python
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
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
>>> import site; site.getsitepackages()
['/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python',
'/Library/Python/2.7/site-packages']
When I try to install numpy via pip it gives me the following response:
Logister-MacBook-Pro:~ Logister$ sudo -H pip install numpy
Requirement already satisfied (use --upgrade to upgrade):
numpy in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
It seems like python 2.7.6 understands where to find numpy, but 2.7.9 does not. Either, how can I point 2.7.9 to the right place, or how can I install numpy so 2.7.9 sees it as well?
Edit: I can run:
site.addsitedir('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python')
In 2.7.9 and then I am able to import numpy. But I dont want to do this every time I launch 2.7.9. Is there a permanent fix? Also, how do I get ipython to run 2.7.9 instead of 2.7.6?
I had a similar problem.
There is two differences versions of python installed on your computer.
Then you´ve installed numpy on python 2.7.6. but the folder of python 2.7.9 haven't numpy. Do you understand?
Other thing your macOS have a groups of variables called "Environment Variables" there it's identifying the command python with python 2.7.9.
Then if you want to use numpy with the interpreter of python do you need change the Enviroment Variables for the command "python" take the version 2.7.6 or install numpy at python 2.7.9 and work with both versions.
Good luck!
With 2 different version you must have 2 python interpreters on your machine. They are installed on different locations with different libraries. So its just right that you cannot find the module installed on interpreter A while using the interpreter B. The which command can be useful to you to figure out where those interpreter are. You can make a symbolic link manually from a folder to another but it would mess up your mind, and you probably gonna get lost later.
I recommed you to install python and ipython via Homebrew, so it would automatically do all the hard work for you.
Install homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Since you said numpy, you are probably looking for scientific stuff so:
# set up some taps and update brew
brew tap homebrew/science # a lot of cool formulae for scientific tools
brew tap homebrew/python # numpy, scipy, matplotlib, ...
brew update && brew upgrade
# install a brewed python
brew install python
Later you can run, but I recommend you to follow this tutorial:
brew install zmq
pip install ipython[all]
I was able to solve the problem by adding the following line to my .bash_profile:
export PYTHONPATH=${PYTHONPATH}:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
I then changed the Ipython version by following this stackoverflow question.
Related
I am getting acquainted with python development and it's been some time since I wrote python code. I am setting up my IDE (Pycharm) and Python3 binaries on Windows 10.
I want to start working with the pytorch library and I am used to before in python2 just typing pip install and it works fine.
Now it seems pip is not installed by default and has been replaced by something called conda?
What's the best way to install the pytorch package from the command line?
here is a screenshot link
PyTorch Documentation does have a selector that will give you the commands for pip and conda.
However, Python3 should have pip installed, it may just be pip3 (that's what it was for me).
if you have multiple version of python installed, you need to call the specific pip of that version to install for that version like
C:\Users\copperfield\Desktop>python -m pip install some_library
you can check the version by
C:\Users\copperfield\Desktop>python --version
but if you have multiples version you might need to append a number to it in order to differentiate between version so call it as python3 or python3x or python3.x where x is the specific version so for example for python 3.10 that is python310 or python3.10
Note, you can enter your python interpreter in your console by simply calling python (or python3 in your case), to get out of it simply call exit()
C:\Users\copperfield\Desktop>python
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>> exit()
C:\Users\copperfield\Desktop>
the pip calls (as show previously) are to be executed in the console, not in the interpreter
First let me start by giving an example of what happens when I install numpy using HomeBrew
brew install numpy
This successfully dowloads and pours. And now when I try to import in python it works properly.
>>>import numpy
>>>
Now I try to install pygtk through HomeBrew using
brew install pygtk
It successfully Downloads and pours. But when I import it in python I get
>>>import pygtk
Import Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pygtk
>>>
Now I went and checked my /usr/local/lib/python2.7/site-packages/
numpy exists but pygtk does not. But pygtk exists in /usr/local/lib/
Why is brew doing this? Also am I getting something fundamentally wrong here?
Now I really don't know what to do exactly but I don't think copying pygtk from /usr/local/lib/ to /usr/local/lib/python2.7/site-packages/
Note: I am using MAC OS and I mainly work through the terminal and would prefer a solution through terminal itself.
Edit1: I have found a solution or rather a workaround.
Basically I am downloading the latest PyGTK.pkg and then installing it. And then copying all the files in the system default python to my python2.7
wget -P /Users/mac_test/Downloads/ https://sourceforge.net/projects/macpkg/files/PyGTK/2.24.0/PyGTK.pkg
installer -pkg /Users/mac_test/Downloads/PyGTK.pkg -target /
cp -r /opt/gtk/lib/python2.7/site-packages/ /usr/local/lib/python2.7/site-packages/
Not sure this is the best way though.
How it worked for me:
First of all I have python installed via brew:
which python && python --version
/usr/local/bin/python
Python 2.7.13
Then I installed pygtk through brew install pygtk and it was successful
After that:
python
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygtk
>>> help(pygtk)
It responds with this:
Help on module pygtk:
NAME
pygtk
FILE
/usr/local/lib/python2.7/site-packages/pygtk.py
So probably copy pygtk to site-packages might solve your issue.
I don't know specific reason you prefer brew over pip + virtualenv but I gently recommend you to try it instead of brew and global library installation.
I'm trying to use pymongo in the IDLE shell on MAC OS X 10.9, but I can only do it in terminal.
when I call import pymongo in IDLE I get the following error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pymongo
ImportError: No module named 'pymongo'
but I use python in terminal I get this:
$ python
Python 2.7.5 (default, Sep 12 2013, 21:33:34)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>>
I can then use it with MongoDB.
Have I made some sort of install error? I'm doing this to learn mongoDB so I'm relatively new to this. Any help is much appreciated.
From the information you supplied in the comments, it appears you have installed a version of Python 3.3.4 on your system but you have installed the PyMongo distribution to an instance of Python 2.7, probably the Apple-supplied system Python 2.7 shipped with OS X 10.9. When you install a third-party package (or "distribution"), it is normally associated only with the Python instance that you used to install it. There are several common ways to install such packages. One way is to use the easy_install command, as is suggested on the PyMongo page. However, the easy_install command is also associated with a particular Python instance. On recent OS X releases, Apple supplies easy_install commands that are associated with and install into the system Pythons. So it's a common pitfall on OS X to install a newer version of Python alongside the system Python but then use the default easy_install command with the result that the package you want ends up installed in the wrong Python version. One solution is to install a separate version of easy_install for each Python version you install. The easy_install command is provided by the setuptools package. However, these days the recommended installer tool for Python is pip which provides more features than easy_install, including the ability to uninstall packages, and is actively supported in the community.
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
python3.3 get-pip.py
python3.3 -m pip install pymongo
python3.3 -c "import pymongo; print(pymongo.version)"
-> 2.6.3
You also need to install a version of pip for each Python instance that you use. There are other ways to invoke pip but, by using the way shown above, you know which version of Python you are using and you are less likely to end up with the situation you have now. There are other tools you can use in addition, like virtualenv, but, particularly on OS X, pip should be sufficient to handle most beginning use cases.
I'm having trouble importing matplotlib.pylab as I get the following:
In [2]: import matplotlib.pylab
Abort trap: 6
I have installed EPD
MacBook-Pro-2:bin$ which python
/Library/Frameworks/EPD64.framework/Versions/Current/bin/python
MacBook-Pro-2:bin$ which ipython
/Library/Frameworks/EPD64.framework/Versions/Current/bin/ipython
And when I run it it works as usual:
MacBook-Pro-2:bin$ ipython
Enthought Python Distribution -- www.enthought.com
Python 2.7.3 |EPD 7.3-2 (64-bit)| (default, Apr 12 2012, 11:14:05)
Type "copyright", "credits" or "license" for more information.
I also cloned matplotlib from github and installed it via the usual python setup.py install but I think my system can't work out which matplotlib to use?
MacBook-Pro-2:bin$ echo $PATH
/Library/Frameworks/EPD64.framework/Versions/Current/bin:
/Users/user/anaconda/bin:/opt/local/bin:/opt/local/sbin:
/opt/local/bin:/opt/local/sbin:/usr/local/share/python:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:
/opt/X11/bin:/usr/local/git/bin:/usr/texbin
I cut it down to:
/Library/Frameworks/EPD64.framework/Versions/Current/bin:
/usr/local/bin:
/usr/bin:/bin:
/opt/X11/bin:
/usr/local/git/bin:
/usr/texbin
but I still get the same error. How could I strip the above down so it just uses the necessary packages within EPD so it doesn't cause a conflict with other python/matplotlib installs? I've looked elsewhere on the web relating to the Abort Trap: 6 error but there isn't anything helpful so far.
Also when I install matplotlib from source It seems to want to write into:
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/
I think this is the cause of the issue. I have manually written into the EPD distro the source matplotlib files.
One final thing which might be related:
I had to use my brew installed python to install wxWidgets (brew install wxWidgets) which installed the following:
/Library/Caches/Homebrew/wxmac-3.0.0.0.tar.bz2
Then I changed $PATH to link back to EPD since it did not have wx installed. I could not install wxWidgets using EPD ipython/python active for some reason. Would not using EPD for this install break anything?
Hi I'm using Ubuntu release 12.10 (quantal) 32-bit with Linux Kernel 3.5.0-21-generic. I'm trying to get IPython's History to work. I've set it up using pythonbrew and a virtual environment. In there I use pip to install IPython. Currently, when I start up IPython in a terminal I get:
WARNING: IPython History requires SQLite, your history will not be saved
Python 2.7.3 (default, Nov 8 2012, 18:25:10)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
Searching on the warning in the first line, I found this issue report, so I went back and installed the following:
sudo apt-get install libsqlite0 libsqlite0-dev libsqlite3-0 libsqlite3-dev
and then removed and reinstalled pysqlite using pip
pip uninstall pysqlite
pip install pysqlite
After that I thought I would check the installation by importing the module:
Python 2.7.3 (default, Nov 8 2012, 18:25:10)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/me/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/home/me/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3
So now it seems the file _sqlite3.so can't be found. That's when I found this SO question. Either it doesn't exist or it's not in my PYTHONPATH environment variable. Searching for the file, I get:
$ locate _sqlite3.so
/home/me/Desktop/.dropbox-dist/_sqlite3.so
/home/me/epd/lib/python2.7/lib-dynload/_sqlite3.so
/usr/lib/python2.7/lib-dynload/_sqlite3.so
So the file is there, but when I looked in my python path:
import sys
for p in sys.path:
print p
none of the above paths that contain _sqlite3.so were contained in my PYTHONPATH. For giggles, I added the path /usr/lib/python2.7/lib-dynload to my PYTHONPATH in a terminal and then tried to import sqlite3 again:
Python 2.7.3 (default, Nov 8 2012, 18:25:10)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append("/usr/lib/python2.7/lib-dynload")
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/me/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/home/me/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: /usr/lib/python2.7/lib-dynload/_sqlite3.so: undefined symbol: PyUnicodeUCS4_DecodeUTF8
Uh oh. Now I'm completely stuck. Can anyone help me out? I've also read in a few places that I may have to rebuild Python. I have no idea how to do this in pythonbrew. Can anyone point me in the right direction?
I've also read in a few places that I may have to rebuild Python.
This is correct. SQLite is part of the standard library,
and is built when you compile Python. There are a few 'optional' parts
of the standard library, which Python will simply skip (with minimal warning, unfortunately)
if the dependencies are missing at build time, and sqlite is one of these.
You should be able to just install libsqlite3-dev,
then rebuild Python and you should be set.
Keep an eye on the build messages,
as they do report which modules they are skipping due to missing dependencies.
Thanks to minrk for pointing me in the right direction. All I had to do was rebuild python. I've outlined the steps below for those that are using pythonbrew. Notice that I already installed the libsqlite3-dev package up in the question section.
First, with the proper version of python and virtual environment loaded up run the command:
$ pip freeze -l > requirements.txt
This gives us a text file list of all of the pip packages that have been installed in the virtual environment for this particular python release in pythonbrew. Then we remove the version of python from pythonbrew and reinstall it (this is the "rebuild python" step):
$ pythonbrew uninstall 2.7.3
$ pythonbrew install 2.7.3
After that, we switch over to the newly installed python version 2.7.3 and create a new virtual environment (which I've called "sci"):
$ pythonbrew switch 2.7.3
$ pythonbrew venv create sci
$ pythonbrew venv use sci
Ideally you should be able to run the command:
$ pip install -r requirements.txt
and according to this pip should reinstall all the modules that you had in the virtual environment before we clobbered that version of python (2.7.3). It didn't work for me for whatever reason so I manually installed all of the modules using pip individuality.
$ ipython --pylab
Python 2.7.3 (default, Jan 5 2013, 18:48:27)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
and IPython history works!
What worked for me (using osx + homebrew + brewed python):
# Reinstall Python 2.7 with sqlite
brew remove python
brew install readline sqlite gdbm --universal
brew install python --universal --framework
# Reinstall iPython with correct bindings
pip uninstall ipython
pip install ipython
And you should be good to go.
You should rebuild your python with sqlite support
sudo apt-get install libsqlite3-dev
wget https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz
tar -xvf Python-2.7.15.tgz
cd Python-2.7.15
./configure
make
sudo make install
Recreate your virtual environment and you should be good to go
rmvirtualenv venv
mkvirtualenv -p python2 venv
workon venv
pip install -r requirements.txt
# or
pip install ipython
This warning appears on macOS when python is installed with pyenv. By default it installs python without sqlite. These commands reinstall python with sqlite support:
pyenv uninstall 3.7
CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install 3.7