57

When installing a new python package with PIP, can I change the package name because there is another package with the same name?

Or, how can I change the existing package's name?

user3562812
  • 1,751
  • 5
  • 20
  • 30
  • 1
    Package names should be unique. Are you trying to upgrade an existing package? – mhawke Dec 05 '14 at 03:43
  • No, they are two different packages for twitter API that have the same name... – user3562812 Dec 05 '14 at 03:45
  • 1
    I agree that package names should be unique. That's great in theory, but is not always so in practice. What is the mechanism for resolving that? I think it would be great if `pip` had the ability to make up for such violations of theory in practice. – Krystian Cybulski Jan 27 '17 at 16:28
  • 1
    I don't think this is really possible, i.e. to have exactly same name for 2 different packages. if that is really the case, can you please give more details of what the packages are? you might want to ask Twitter why they released different packages under same name. – stucash Nov 27 '17 at 16:58
  • 2
    @stucash it can happen, E.g. https://github.com/jpadilla/pyjwt, https://github.com/GehirnInc/python-jwt, se the import and the from import statement, these two collide, I'm facing this issue right now as I need a function from one and another function fro the other – Shailyn Ortiz Dec 28 '18 at 14:47
  • @ShailynOrtiz The question needs clarifying then because those packages still have different names on PIP – Chris_Rands Apr 24 '19 at 11:28
  • @Chris_Rands yes, but on local they share the same name – Shailyn Ortiz Apr 24 '19 at 14:27
  • 2
    For clarification, there are two names to distinguish: The **distribution name** (≈ project name) is the one indexed by PyPI and that you use to install via `pip`. PyPI ensures uniqueness. The **package name** was set by the package publisher. Uniqueness of package names are recommended (see [PEP423](https://www.python.org/dev/peps/pep-0423/#use-a-single-name)), but not ensured. Therefore, distribution name and package name can differ. For example `pip install "scikit-learn"` vs. `import sklearn`. See this glossary for a [distinction](https://packaging.python.org/glossary/). – normanius Dec 06 '20 at 22:50

9 Answers9

18

It's not possible to change "import path" (installed name) by specifying arguments to pip. All other options require some form of "changes to the package":

A. Use pip install -e git+http://some_url#egg=some-name: that way even if both packages have the same import path, they will be saved under different directories (using some-name provided after #egg=). After this you can go to the source directories of packages (usually venv/src/some-name) and rename some folders to change import paths

B-C. Fork the repository, make changes, then install the package from that repository. Or you can publish your package on PyPI using different name and install it by name

D. use pip download to put one of the packages in your project, then rename folders as you like

imposeren
  • 4,142
  • 1
  • 19
  • 27
8

I think one way of going about this can be using

pip download

See the docs here. You can change the name of the package after it has been downloaded and then go about manually installing it. I haven't tested this but seems like it should work.

unixia
  • 4,102
  • 1
  • 19
  • 23
4

Create a new virtualenv and then install the package on new virtualenv, with this you can have the different version of packages as well.

Razia Khan
  • 493
  • 7
  • 14
  • It doesn't answer the question: "how to install with a different name". – Vladimir Obrizan Apr 25 '22 at 12:04
  • @VladimirObrizan you can use virtualenv & virtualenvwrapper library if you want to install with a different name -- first read documentation properly than follow the doc steps. https://pypi.org/project/virtualenvwrapper/ https://pypi.org/project/virtualenv/ – Razia Khan Apr 25 '22 at 12:10
2

If you are struggling to install the correct package when using pip install 'module', you could always download its corresponding wheel file (.whl extension) and then install this directly using pip. This has worked for me in various situations in the past.

Rob123
  • 895
  • 6
  • 10
2

Use virtualenv if you don't need both package for the same project. With virtualenv you can have different version of packages as well.

Another way maybe the site-packages as mentioned already.

https://virtualenv.pypa.io/en/stable/

http://docs.python-guide.org/en/latest/dev/virtualenvs/#lower-level-virtualenv

2

I had this problem with the libraries gmail and pygmail, they both want to install to PYTHONPATH/site-packages/gmail/. Clearly the pygmail package has an issue, it should be installing to pygmail folder, but they haven't made any updates in years.

For an interim solution, I installed one (pygmail), then changed the folder names (gmail-->pygmail, and gmail-v#.dist-info-->pygmail-v#.dist-info), then installed the second one normally. Seems to work, as long as I don't try to update the first package. import gmail and import pygmail work as expected.

Erock618
  • 453
  • 4
  • 10
2

I'm guessing that at least one of the packages you refer is not installed from https://pypi.org/ since they enforce unique names for the packages; this means you're installing that package from source, which means you also have the freedom of changing its name to whatever you want. You would do this by changing the setup.py file in the root of the offending package.

vlsd
  • 945
  • 6
  • 18
  • I see what you are getting at here but I'm wondering if there's any way to proceed that doesn't involve creating a fork. – shuttle87 Apr 28 '19 at 11:25
  • @shuttle87 I would argue this is the clearest case of when creating a fork (and possible pull request) is the right way to go. You have discovered a bug in an open source repo and you are in a unique position to fix it... you don't *have* to do it, but it's a relatively easy lift that will benefit the open source community at large. – vlsd Apr 29 '19 at 16:25
  • 1
    @vlsd I'm not sure if your reasoning in the answer is correct. The distribution names are unique, but the package names not necessarily. [Glossary](https://packaging.python.org/glossary/) – normanius Dec 06 '20 at 22:17
  • Good point @normanius I hadn't even thought of that since most (but not all) packages have the same distribution and package name... I wonder why they don't enforce unique package names – vlsd Dec 09 '20 at 16:57
  • It's recommended ([PEP423](https://www.python.org/dev/peps/pep-0423/)), but not enforced. Technically, it's possible to bundle multiple packages into the same distribution. I guess that's why different package names are permitted. Another reason is that it permits projects/distributions to drop-in replace existing packages from other distributions. But I agree, Python is a bit messy with regard to its package infrastructure, and IMHO they could have done more to keep things clean. – normanius Dec 09 '20 at 17:25
0

I Don't think it is possible to change the name of package by using pip. Because pip can install packages which are exist and gives error if there is no package name which you write for change the name of package.

Pooa
  • 9
  • 1
  • 11
-2

This is not possible with the pip command line tool. All of the packages on PyPI have unique names. Packages often require and depend on each other, and assume the name will not change.

AlexMeng
  • 833
  • 8
  • 15
  • 4
    Someone should reconsider that – Lantern Oct 03 '15 at 17:05
  • 10
    That is not true. I installed `django-emoji` and `emoji`. To import from each you need to import from `emoji`. See examples on https://pypi.python.org/pypi/django-emoji and https://pypi.python.org/pypi/emoji. How do we go about solving this? – Krystian Cybulski Jan 27 '17 at 16:01
  • @KrystianCybulski could you rephrase what you meant by 'To import from each you need to import from emoji', not sure why you meant here? – stucash Nov 27 '17 at 17:01
  • 2
    `import emoji` to use either of those libraries, what if you need both? – Shailyn Ortiz Dec 28 '18 at 14:48