Modules in Python linux

Asked

Viewed 1,336 times

2

I’m having some problems installing modules in my python on linux. I’m currently using python 2.7.10, most modules I use Pip to install, but when I run python 2.7.10 and import my module it says it didn’t, but if I use version 2.7.6 (/usr/bin/python2.7) they work, it seems that when I install these modules are visible only to previous version.

Before 2.7.10 I was with 2.7.9 and the same problem happened. Does anyone know what it can be ?

  • I think what’s happening is that the pip which is being used is loading the wrong Python, possibly /usr/bin/python which should be a symlink to the old version. Use ls -l /usr/bin/python to see if this is really what it is. If that’s all you need to do is redo the symlink for your new Python.

2 answers

1

You probably have two installations of Python - the 2.7.6 must be the one that came with your Linux distribution, and the 2.7.10, which I imagine you installed by hand, in some kind of corner /usr/local/?

You can use the virtualenv to manage the various facilities - it’s the ideal method, really - but if you want to do something dirty, just use

/usr/local/bin/python2.7 -m pip

instead of pip whenever you install a package; this will ensure that it will be installed in python 2.7.10, not python 2.7.6.

  • I did not find anything in place, probably I have there another version yes, but I would not have a way to delete version 2.6 and leave as default of my system the 2.7.10 ?

  • Apparently the answer is "yes", but it involves a certain job (and I would take a backup of your important things before trying).

  • +1 for the use of Virtualenv. Unless you are setting up a dedicated server, the simplest thing to do and maintain is to use Virtualenv.

0

S and its system came with Python 2.7, it is possible to keep it with Python 2.7.10. and your system came with Python 2.6, there is no way to change the Python system standard to 2.7: you will destroy your system - most Linux distributions have several key administration programs that rely heavily on Python in the system.

The correct way to proceed is: use the system manager to download the prerequisites to compile Python (for example, in Debian, Ubuntu and derivatives: "apt-get build-Dep python-2.7", in Folder: yum-builddep python2). -Download the source code (the .tar.gz file) of Python.org, and Compile a local version, for the user - set the prefix to be within your own home (./configure --prefix=/home/user/usr) or a system prefix, separate of /usr - /opt is a good call. The advantage of using a prefix within your /home is that you do not need super-user permissions at any time (sudo) to install the new version of Python.

From there, install the virtualenv package in the system’s Python, and for each project you work on, create a virtualenv, using the Python that Voce compiled - for that use the argument -p of virtualenv. Each time you work on the project, activate virtualenv. The sequence, after compiling the new Python, is this:

apt-get install python-virtualenv
virtualenv -p /home/usuario/usr/bin/python-2.7 meuprojeto
cd meuprojeto
source bin/activate

and from there put the . py of your new project inside the - folder and install other modules at will with the command pip: the installed modules will be restricted to virtualenv. A second project that uses different versions of the same modules can be created in another folder, and one project will not interfere with the other.

(The step source bin/activate should run on every shell you run Python programs from this project - there are auxiliary virtualenv packages that allow a simpler command to activate the virtualenvs, but the difference is almost cosmetic only)

This recipe works in the same way for newer versions of Python - and you can even have separate virtualenvs with Python 3.4 and Python 3.5 beta.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.