Python 2.7 and 3.4: Libraries inaccessible across versions - python

I am new to Python. I am running Ubuntu 14.04, and I have both Python 2.7 and 3.4 on it.
I want to use the newer 3.x version, with the NumPy, SciPy, and NLTK libraries. I set the Python REPL path to Python 3.x in the ~/.bash_aliases file like so:
alias python=python3
After this I installed several libs, including python-numpy, python-scipy, and python-matplotlib.
$ sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
Unfortunately, I am facing issues since I am guessing that the libraries got installed for the older 2.7 version of Python; I am unable to access the libraries using the 3.4 REPL.
import numpy
ImportError: No module named 'numpy'
However, I am able to access the libraries using the older version:
$ /usr/bin/python2.7
How do I get this this work?

When you install Python packages using apt-get, you're relying on the distribution package manager. The Ubuntu convention is to prefix Python 2 packages with python-, and Python 3 packages with python3-.
This distinction is necessary because Python 3 introduced some incompatible changes from Python 2. It's thus not possible to simply recompile (most) packages for Python 3, meaning both need to be made available.
Alternatively, you can use the Python package manager, pip (or pip3). The catch with this is that some packages (like scipy) require certain compiler toolchains which you might not have installed.
It's generally a good idea to stick to either apt-get or pip for a particular machine. You probably won't have issues if you mix them, but it's better to be consistent.

Related

Can installing two python versions can cause problems?

I've install 3.7.9 and 3.9.7 and installed some packages (including pandas and numpy) but after doing so running or using them in programs some of them are executable in first or second versions of the python how to overcome this ?
Should I just delete any one of the version or there is any remedy for this
You can install multiple python versions without a problem. Whenever you install a package though, it will only be installed into one of the two python versions. You therefore need to install once for python 3.7.9, and once for python 3.9.7. Depending on which OS you are running, you can run the different versions through python3.9 or python3.7. Lets say you want to install pandas on python3.9, you run python3.9 -m pip install pandas.
In general, it is a good idea to use virtual environments (see venv for example). This will install all the dependencies that you have for a project separately. In the long run, such approach will avoid problems with compatibility after upgrading to newer python versions or package versions.

Remove old Python && Fresh install Python

AttributeError: 'module' object has no attribute 'openssl_md_meth_names'
I keep receiving this error when trying to install Flask or any library through pip
I have viewed other attributeerror posts and they have helped but this openssl module has little insight. I did try to install Python 2.7.9
What steps should I take to remove python and preform a fresh install? I executed sudo apt-get remove python earlier and that was a big mistake. I have since recovered from that.
I would try:
sudo apt-get install --reinstall python2.7
I would also then use python2.7 instead of python in the command line to be sure I'm using the correct executable.
What I would do in your case to avoid future headaches is install python via anaconda. Tutorial here: http://docs.continuum.io/anaconda/
With this tool you install python in a different place than the system python and can use different versions and different packages for different projects without breaking your system (although it does have a learning curve). It also has good support for libraries like numpy and the like who can get grumpy when compiled and you can use pip with it.
If you use apt-get to install Python, then you get whatever version of Python is supported by your Linux distribution.
If you are using Debian, you can get 2.7.9 from the "unstable" repos (code-named "sid"). You don't necessarily have to upgrade your whole system to "unstable"; read about "pinning" packages. Pinning is tricky and I avoid it.
You also have the option of simply downloading the Python source code and building it on your system. Then you would run Python 2.7.9 out of /usr/local/bin and you would still have the stable Python supported by your system.
If you do wind up installing your own build of Python, you might want to look into VirtualEnv. http://simononsoftware.com/virtualenv-tutorial-part-2/

Python3.2 can not recognize UP/DOWN/LEFT/RIGHT keys in interpreter?

I am using ubuntu, linux kernel 2.6.38. I usually use python2, today, I decide to try Python3. I downloads python3 and make install it following the README. However,the python 3.2 interpreter can not recognize UP/SOWN/LEFT/RIGHT keys, these keys are available in my python 2.7 interpreter. What's wrong did I make?
Another question is can I choose the python version which iPython use if I have python2.7 and python3.2 at the same time.
Best Regards.
This happens if the GNU Readline Library is not installed. Install the development version of the Readline library and recompile. (Some Linux distros have different packages for the development version and the runtime version of a library. The development version is needed to compile packages which use the library.)
On Debian-derived distros like Ubuntu, the package is libreadline-dev.
sudo apt install libreadline-dev
On Redhat-derived distros like Fedora, the package is named readline-devel.
sudo dnf install readline-devel
You may be able to use the Editline library instead of Readline. It’s basically a different library that does the same thing.
This happens when compiling python without readline support.
Install the readline developement pakckages so that the readline.so module gets build when compiling.
If you want to use ipython on both python2 and python3, you'll have to install it separately for each interpreter.
One last hint: the ubuntu already has python3 in it's repository. you can install it using:
sudo apt-get install python3 ipython3

I want to use matplotlib in osx lion

I installed matplotlib from macports, and version of python is 2.7.1.
$ sudo port install py27-matplotlib py27-matplotlib-basemap
I wrote a sample program below.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from pylab import *
plot([1,2,3])
show()
But this didn't work correctly with error log "no module named pylab".
And I tried easy_install.
$ sudo easy_install matplotlib
In this case, my program worked correctly with no errors.
What is the difference between these two cases(macports and easy_install)?
I think the version of matplotlib is same in two cases.
In case of macports, do I have to redefine include path?
Would you help me??
To ensure a compatible environment, MacPorts Python packages automatically install a MacPorts Python. By default, you can invoke it via /opt/local/bin/python2.7. And that's where you will find the MacPorts installed matplotlib et al. /usr/bin/python will invoke the Apple-supplied system Python 2.7.1 and, when you ran sudo easy_install you were using the Apple-supplied easy_install command associated with the system Python. That means you now have two Python 2.7 instances installed, each with a separate version of matplotlib. There's nothing wrong with that but you probably want to stick with one or the other. You can make the MacPorts Python 2.7 be your default by ensuring your SHELL path has /opt/local/bin before /usr/bin and by using the MacPorts port select python python27 command.
As mentioned by Ned Deily, the problem is caused because MacPorts installs a separate Python even though OS X ships its own version.
I would suggest you use Homebrew instead of MacPorts to avoid problems like this. Homebrew will use the available package on OS X when possible.
Another suggestion is that it's better to use pip to manage Python package, which is a replacement for easy_install and supports uninstalling packages. The benefit of using packaging system (like MacPorts, Debian's apt) to manage python package is that they can solve the dependency if the Python package relies on other C libraries. But in case the some Python packages are not included in MacPorts or apt, you will need to resolve to easy_install or pip. And it's usually not a good idea to use two packaging system to manage your python package at the same time.

What is the most compatible way to install python modules on a Mac?

I'm starting to learn python and loving it. I work on a Mac mainly as well as Linux. I'm finding that on Linux (Ubuntu 9.04 mostly) when I install a python module using apt-get it works fine. I can import it with no trouble.
On the Mac, I'm used to using Macports to install all the Unixy stuff. However, I'm finding that most of the python modules I install with it are not being seen by python. I've spent some time playing around with PATH settings and using python_select . Nothing has really worked and at this point I'm not really understanding, instead I'm just poking around.
I get the impression that Macports isn't universally loved for managing python modules. I'd like to start fresh using a more "accepted" (if that's the right word) approach.
So, I was wondering, what is the method that Mac python developers use to manage their modules?
Bonus questions:
Do you use Apple's python, or some other version?
Do you compile everything from source or is there a package manger that works well (Fink?).
The most popular way to manage python packages (if you're not using your system package manager) is to use setuptools and easy_install. It is probably already installed on your system. Use it like this:
easy_install django
easy_install uses the Python Package Index which is an amazing resource for python developers. Have a look around to see what packages are available.
A better option is pip, which is gaining traction, as it attempts to fix a lot of the problems associated with easy_install. Pip uses the same package repository as easy_install, it just works better. Really the only time use need to use easy_install is for this command:
easy_install pip
After that, use:
pip install django
At some point you will probably want to learn a bit about virtualenv. If you do a lot of python development on projects with conflicting package requirements, virtualenv is a godsend. It will allow you to have completely different versions of various packages, and switch between them easily depending your needs.
Regarding which python to use, sticking with Apple's python will give you the least headaches, but If you need a newer version (Leopard is 2.5.1 I believe), I would go with the macports python 2.6.
Your question is already three years old and there are some details not covered in other answers:
Most people I know use HomeBrew or MacPorts, I prefer MacPorts because of its clean cut of what is a default Mac OS X environment and my development setup. Just move out your /opt folder and test your packages with a normal user Python environment
MacPorts is only portable within Mac, but with easy_install or pip you will learn how to setup your environment in any platform (Win/Mac/Linux/Bsd...). Furthermore it will always be more up to date and with more packages
I personally let MacPorts handle my Python modules to keep everything updated. Like any other high level package manager (ie: apt-get) it is much better for the heavy lifting of modules with lots of binary dependencies. There is no way I would build my Qt bindings (PySide) with easy_install or pip. Qt is huge and takes a lot to compile. As soon as you want a Python package that needs a library used by non Python programs, try to avoid easy_install or pip
At some point you will find that there are some packages missing within MacPorts. I do not believe that MacPorts will ever give you the whole CheeseShop. For example, recently I needed the Elixir module, but MacPorts only offers py25-elixir and py26-elixir, no py27 version. In cases like these you have:
pip-2.7 install --user elixir
( make sure you always type pip-(version) )
That will build an extra Python library in your home dir. Yes, Python will work with more than one library location: one controlled by MacPorts and a user local one for everything missing within MacPorts.
Now notice that I favor pip over easy_install. There is a good reason you should avoid setuptools and easy_install. Here is a good explanation and I try to keep away from them. One very useful feature of pip is giving you a list of all the modules (along their versions) that you installed with MacPorts, easy_install and pip itself:
pip-2.7 freeze
If you already started using easy_install, don't worry, pip can recognize everything done already by easy_install and even upgrade the packages installed with it.
If you are a developer keep an eye on virtualenv for controlling different setups and combinations of module versions. Other answers mention it already, what is not mentioned so far is the Tox module, a tool for testing that your package installs correctly with different Python versions.
Although I usually do not have version conflicts, I like to have virtualenv to set up a clean environment and get a clear view of my packages dependencies. That way I never forget any dependencies in my setup.py
If you go for MacPorts be aware that multiple versions of the same package are not selected anymore like the old Debian style with an extra python_select package (it is still there for compatibility). Now you have the select command to choose which Python version will be used (you can even select the Apple installed ones):
$ port select python
Available versions for python:
none
python25-apple
python26-apple
python27 (active)
python27-apple
python32
$ port select python python32
Add tox on top of it and your programs should be really portable
Please see Python OS X development environment. The best way is to use MacPorts. Download and install MacPorts, then install Python via MacPorts by typing the following commands in the Terminal:
sudo port install python26 python_select
sudo port select --set python python26
OR
sudo port install python30 python_select
sudo port select --set python python30
Use the first set of commands to install Python 2.6 and the second set to install Python 3.0. Then use:
sudo port install py26-packagename
OR
sudo port install py30-packagename
In the above commands, replace packagename with the name of the package, for example:
sudo port install py26-setuptools
These commands will automatically install the package (and its dependencies) for the given Python version.
For a full list of available packages for Python, type:
port list | grep py26-
OR
port list | grep py30-
Which command you use depends on which version of Python you chose to install.
I use MacPorts to install Python and any third-party modules tracked by MacPorts into /opt/local, and I install any manually installed modules (those not in the MacPorts repository) into /usr/local, and this has never caused any problems. I think you may be confused as to the use of certain MacPorts scripts and environment variables.
MacPorts python_select is used to select the "current" version of Python, but it has nothing to do with modules. This allows you to, e.g., install both Python 2.5 and Python 2.6 using MacPorts, and switch between installs.
The $PATH environment variables does not affect what Python modules are loaded. $PYTHONPATH is what you are looking for. $PYTHONPATH should point to directories containing Python modules you want to load. In my case, my $PYTHONPATH variable contains /usr/local/lib/python26/site-packages. If you use MacPorts' Python, it sets up the other proper directories for you, so you only need to add additional paths to $PYTHONPATH. But again, $PATH isn't used at all when Python searches for modules you have installed.
$PATH is used to find executables, so if you install MacPorts' Python, make sure /opt/local/bin is in your $PATH.
There's nothing wrong with using a MacPorts Python installation. If you are installing python modules from MacPorts but then not seeing them, that likely means you are not invoking the MacPorts python you installed to. In a terminal shell, you can use absolute paths to invoke the various Pythons that may be installed. For example:
$ /usr/bin/python2.5 # Apple-supplied 2.5 (Leopard)
$ /opt/local/bin/python2.5 # MacPorts 2.5
$ /opt/local/bin/python2.6 # MacPorts 2.6
$ /usr/local/bin/python2.6 # python.org (MacPython) 2.6
$ /usr/local/bin/python3.1 # python.org (MacPython) 3.1
To get the right python by default requires ensuring your shell $PATH is set properly to ensure that the right executable is found first. Another solution is to define shell aliases to the various pythons.
A python.org (MacPython) installation is fine, too, as others have suggested. easy_install can help but, again, because each Python instance may have its own easy_install command, make sure you are invoking the right easy_install.
If you use Python from MacPorts, it has it's own easy_install located at: /opt/local/bin/easy_install-2.6 (for py26, that is). It's not the same one as simply calling easy_install directly, even if you used python_select to change your default python command.
Have you looked into easy_install at all? It won't synchronize your macports or anything like that, but it will automatically download the latest package and all necessary dependencies, i.e.
easy_install nose
for the nose unit testing package, or
easy_install trac
for the trac bug tracker.
There's a bit more information on their EasyInstall page too.
For MacPython installations, I found an effective solution to fixing the problem with setuptools (easy_install) in this blog post:
http://droidism.com/getting-running-with-django-and-macpython-26-on-leopard
One handy tip includes finding out which version of python is active in the terminal:
which python
When you install modules with MacPorts, it does not go into Apple's version of Python. Instead those modules are installed onto the MacPorts version of Python selected.
You can change which version of Python is used by default using a mac port called python_select. instructions here.
Also, there's easy_install. Which will use python to install python modules.
You may already have pip3 pre-installed, so just try it!
Regarding which python version to use, Mac OS usually ships an old version of python. It's a good idea to upgrade to a newer version. You can download a .dmg from http://www.python.org/download/ . If you do that, remember to update the path. You can find the exact commands here http://farmdev.com/thoughts/66/python-3-0-on-mac-os-x-alongside-2-6-2-5-etc-/
I use easy_install with Apple's Python, and it works like a charm.
Directly install one of the fink packages (Django 1.6 as of 2013-Nov)
fink install django-py27
fink install django-py33
Or create yourself a virtualenv:
fink install virtualenv-py27
virtualenv django-env
source django-env/bin/activate
pip install django
deactivate # when you are done
Or use fink django plus any other pip installed packages in a virtualenv
fink install django-py27
fink install virtualenv-py27
virtualenv django-env --system-site-packages
source django-env/bin/activate
# django already installed
pip install django-analytical # or anything else you might want
deactivate # back to your normally scheduled programming

Categories

Resources