问题
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?
回答1:
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
- Getting started with pip and virtualenv
- virtualenv official documentation
- pip user guide (installing packages and dealing with versions)
- Pip quickstart
and finally I warmly recommend the Hitchhiker's guide to python helped me a lot to get up to speed.
来源:https://stackoverflow.com/questions/48332046/does-pip-python-support-multiple-versions-of-the-same-package