Domanda

If I install several packages with Python 2.6 (e.g. using easy_install) and then I want to upgrade to Python 2.7, is there a way to upgrade Python and then automatically "import" all those installed packages along with it? Or do they have to be reinstalled?

Two related questions: (1) if a package is installed in a Python 2.6 packages directory, is it legitimate to import it into the PYTHONPATH of a newer Python, like Python 2.7, or must all the packages be reinstalled with Python 2.7? (2) if I use easy_install, how can I tell it to use the newer Python? E.g. 2.7 instead of 2.6? Or should I just reinstall easy_install using Python 2.7 to do this? thanks.

È stato utile?

Soluzione

First, this is one of the many reasons you want to use pip instead of easy_install. (You still need easy_install to get pip itself, but beyond that, don't touch it ever again.) If you'd used pip, you could just do this:

pip freeze > modules.dump

That gives you a list of all of the modules you have installed, and their version numbers. Most of the time, you can just take the list of modules (line.split('==')[0] for line in f) and pass it to pip install.

But that's for future reference. For today, you have to piece it together yourself by looking through your site-packages directory. Fortunately, many things will end up as foo_bar-1.2.3.4-blah-blah.egg, so all you have to do is guess whether the package is named foo-bar or foo_bar at PyPI, and usually even if you guess wrong, easy_install or pip will get the right thing anyway. So, you can't quite automate it, but you can get close.

But yes, however you do it, you do need to reinstall. Anything that requires C extension code has to be recompiled. Pure-Python packages may not need to be changed, but they may, and you're better safe than sorry. Also, if you try to copy some things over but not others, you're going to make a big mess of your dependencies.

(1) if a package is installed in a Python 2.6 packages directory, is it legitimate to import it into the PYTHONPATH of a newer Python, like Python 2.7, or must all the packages be reinstalled with Python 2.7?

Don't do that; reinstall them, as explained above.

(2) if I use easy_install, how can I tell it to use the newer Python? E.g. 2.7 instead of 2.6? Or should I just reinstall easy_install using Python 2.7 to do this? thanks.

You need the 2.7 easy_install. You can usually use a 2.7 easy_install with 2.6 by running, e.g., python2.6 $(which easy_install), but the other way around isn't guaranteed to work.

And you don't want to do that anyway. If you want two versions of Python in parallel, you want two versions of easy_install—normally you want to end up with easy_install-2.6 and easy_install-2.7, with easy_install as a symlink to whichever one you consider your "primary" python.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top