简体   繁体   中英

Use different version of python package in the same environment

I need to have different versions of elasticseach-dsl installed in the same virtualenv. I would like to be able to import them using some sort of alias:

import elasticsearch_dsl1
import elasticsearch_dsl2
import elasticsearch_dsl5

I was thinking to create packages called elasricsearch_dsl* and in their setup.py add install requirements for the proper version of elasticsearch_dsl but when I install the packages it results in a conflict because all my packages require different versions of the same library. I have no clue on how to proceed and if this is possible.

Thanks a lot to everyone

Short answer:

It is not possible.

I was able to find a solution to my problem.

  • I downloaded the source code of the elasticsearch-dsl libraries of different versions
  • Then changed the import statements to use elasticsearch1 , elasticsearch2 etc.libraries instead of elasticsearch v1.x and v2.x
# example `elasticsearch-dsl` v5.4.0
from elasticsearch import Elasticsearch #  --> from elasticsearch5 import Elasticsearch
  • Modified the setup.py to change the dependencies from elasticsearch to elasticsearch1 , elasticsearch2 and so on for each version of the elasticsearch-dsl

I've been very lucky because of the existence of elasticsearch1 , elasticsearch2 ... otherwise I would have to repeat the same procedure for them. And more lucky for the compatibility of dependencies of the various versions of the library.

I'm not proud of this hack but it worked.

setup.py

An example of a setup.py file for elasticsearch-dls==7.3.0 , then elasticsearch-dsl7

from os.path import join, dirname
from setuptools import setup, find_packages

VERSION = (7, 3, 0)
__version__ = VERSION
__versionstr__ = ".".join(map(str, VERSION))

f = open(join(dirname(__file__), "README"))
long_description = f.read().strip()
f.close()

install_requires = [
    "six",
    "python-dateutil",
    "elasticsearch7",  # before "elasticsearch>=7.0.0,<8.0.0"    <---
    # ipaddress is included in stdlib since python 3.3
    'ipaddress; python_version<"3.3"',
]

tests_require = [
    "mock",
    "pytest>=3.0.0",
    "pytest-cov",
    "pytest-mock<3.0.0",
    "pytz",
    "coverage<5.0.0",
]

setup(
    name="elasticsearch-dsl7", #    <---
    description="Python client for Elasticsearch",
    license="Apache-2.0",
    url="https://github.com/elasticsearch/elasticsearch-dsl-py",
    long_description=long_description,
    version=__versionstr__,
    author="Honza Král",
    author_email="honza.kral@gmail.com",
    maintainer="Seth Michael Larson",
    maintainer_email="seth.larson@elastic.co",
    packages=find_packages(where=".", exclude=("test_elasticsearch_dsl*",)),
    python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
    classifiers=[
        "Development Status :: 4 - Beta",
        "License :: OSI Approved :: Apache Software License",
        "Intended Audience :: Developers",
        "Operating System :: OS Independent",
        "Programming Language :: Python",
        "Programming Language :: Python :: 2",
        "Programming Language :: Python :: 2.7",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.4",
        "Programming Language :: Python :: 3.5",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: Implementation :: CPython",
        "Programming Language :: Python :: Implementation :: PyPy",
    ],
    install_requires=install_requires,
    test_suite="test_elasticsearch_dsl.run_tests.run_all",
    tests_require=tests_require,
    extras_require={"develop": tests_require + ["sphinx", "sphinx_rtd_theme"]},
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM