Does PIP/Python support multiple versions of the same package?
Asked Answered
V

1

7

Let's say I have a package foo, and foo packages up binary shared objects that I use in multiple Python scripts.

  • Foo v1 (shared objects)
    • Bar v1 (requires Foo v1)
    • Baz v1 (requires Foo v1)

Now I want to push out a new breaking update to Foo.

  • Foo v2 (shared objects)
    • Bar v2 (requires Foo v2)
  • Foo v1 (shared objects)
    • Bar v1 (requires Foo v1)
    • Baz v1 (requires Foo v1)

Can old-dependencies with different major versions in semvar be distributed over PyPI? If so, how?

Vehicle answered 18/1, 2018 at 23:39 Comment(7)
Bar v2 (requires Foo v2) I suppose?Rima
@Rima Yes, that is correctVehicle
Then edit and fix you question, please.Rima
I don't understand the question though. PyPI doesn't automatically remove packages or versions, you can store as many versions as you want.Rima
@Rima you mean when you install a new version of a python module, it maintains the old version too? I don't see that.. Looking at my site-packages/ I see modules by name without version numbers entirely.Vehicle
So the question is about installing two versions in parallel, not publishing them at PyPI? Well, it's also possible but requires manipulations with sys.path. IMO the best way is to use different virtual environments for different versions.Rima
Anyway 2 different versions of a package cannot be used in one program.Rima
D
7

Pypi can and will track multiple versions of the same package without issues. You can also explicitly reference a specific version of a package when installing a package with pip.

However a given environment (or virtual environment) will only carry a single version of a given package. If you ask pip to update BAR to V2 then FOO will also be updated and the FOO V1 will be replaced.

To develop with FOO V1 and FOO V2 concurrently on the same computer you would have to use virtualenv and create a separate virtual environment where each version of FOO will reside without interfering. Each will have it's own site-packages carrying each one specific version of the package.


Virtualenv will create alternate "installations" of python using the main installation as a model. You can then switch to the virtualenv and install any package you want in there and they will remain with this specific environment.

deactivate will return you to the global environment.

Create a second environment and install a different set of packages in there.

Then you can easily switch from one to the other by running the activate script from each environment (depending on your platform the actual script might differ slightly, under windows it will be in env-root/Scripts/activate.bat or activate.ps1 if you prefer working from powershell)

Install virtualenv

pip install virtualenv

create an environment FIZ in the current folder

virtualenv FIZ

Activate this environment (assuming windows normal shell)

FIZ\Scripts\activate.bat

you will see your prompt change adding FIZ stating you are in this environment. Anything you install will be limited to that FIZ environment and will only be available once it is activated.

pip install click

For example will install click (a library to help create command line interface) at the latest version.

pip install click==6.1

will remove whatever version of click and replace with version 6.1 explicitely

deactivate the environment

deactivate

create a second environment FUZ

virtualenv FUZ

activating this environment will allow you to install a different version of click (or whatever) than was present in FIZ and both will live concurently on your computer but only one can be used at any given time. Though technically you could open two shell windows and have both environment alive at the same time.

Hope this helps !

Here some additional reading on the subject

and finally I warmly recommend the Hitchhiker's guide to python helped me a lot to get up to speed.

Distributee answered 19/1, 2018 at 17:12 Comment(2)
Do you have any pointers on how to do this. This is exactly what I'm looking for. Assuming no knowledge of virtualenv, is it easy to show in the answer? Or could you link me something to read. I know there is a pip and a new official pipenvVehicle
Added some more info, hope this helps!Distributee

© 2022 - 2024 — McMap. All rights reserved.