How to specify version of module installed from git repo with pip install -r requirements.txt
Asked Answered
S

2

0

In my Python FastAPI app, I need Pydantic version 2 to solve an issue with Unions that is fixed in version 2 only.

Since FastAPI version 0.100.0 and onward supports Pydantic v2, I was first hopeful that depending on Pydantic v2 would work fine, however another indirect depdendency in my project called sqlmodel specifies to only support Pedantic 1.x.

sqlmodel is incidently also written by tiangolo which is the author of FastAPI and he stated that sqlmodel would get a version bump with support for Pydantic v2, the discussion can be seen here. But this has taken some time and has caused a lot of problems for many users and prompted someone to create a fork of sqlmodel, patching it to support Pydantic v2.

I want to use this forked version of sqlmodel in my project to get Pydantic v2 support installed without the dependency problems. I know it is possible to specify a git repo directly in my requirements.txt file using the -e git+https://github.com/author/project.git@branch_tag_or_hash#egg=packagename syntax, however when I try this with the fork of sqlmodel I get an error because now sqlmodel has version "0" according to pip.

The exact syntax I have used in my requirements.txt looks like this:

-e git+https://github.com/honglei/sqlmodel.git@main#egg=sqlmodel

and the exact error message after running pip install -r requirements.txt looks like this:

ERROR: Cannot install sqlmodel 0 (from git+https://github.com/honglei/sqlmodel.git@main#egg=sqlmodel) because these package versions have conflicting dependencies.

As you can clearly see, pip thinks we are installing "version 0" of sqlmodel now which is clearly not the case.

So there are two questions;

  1. How can I convince pip that the git fork of sqlmodel is actually version 0.0.9 or some other arbitrary value that makes it be accepted as the latest version of the package during dependency resolution?

  2. Taking a step back, are there any more elegant ways to resolve this? It seems quite radical and hacky to depend on a fork in github, but I don't see any other way of having FastAPI and Pydantic v2 in my app at this point.

Systematic answered 29/9, 2023 at 22:9 Comment(3)
Regarding question 2: the underlying issue is that SQLModel is a severely underdeveloped library that hasn't had a release in over a year. I used it when it first came out, and since then, SQLAlchemy (the library SQLModel is based on, which handles all database interactions) has improved their typing support a lot. I ended up ditching SQLModel, I would recommend you do the same, at least until it at the very least gets a 0.1.0 release.Jumpy
@Jumpy I would love to ditch sqlmodel however it is an indirect dependency in my project. I don't even know which package depends on it. I will surely make a search to see if I can cut it out completely.Systematic
You could have a look at pipdeptree to view the dependency tree and see which package depends on it.Jumpy
J
1

See this answer for the syntax. Also, you don't need -e, that's for making the install editable, which is only useful when you’re working on that package yourself.

Jumpy answered 29/9, 2023 at 22:34 Comment(2)
Thank you for the answer, however it does not solve my problem. It is kind of hard to explan, but let me try: The latest version of sqlmodel in pypi is 0.0.8. Other packages in pypi that depend on sqlmodel will have a dependency saying something like "sqlmodel>=0.0.8". The git repo has no tags or branches even resembling a clean version name. The only branch there is "main". Dependency resolver will NOT think "main>=0.0.8", therefore my git package is not selected and dependency resolution fails. How can I give my git package a made up version number that will make dependency resolution work?Systematic
Ah, I see that you solved it. Another solution probably would have been to uninstall the pypi version and then install the fork.Jumpy
S
0

I will add the answer that solved the problem for me in case someone else faces the same issue. It is not elegant and I hope there is another better solution out there. Please share if you know of one.

Here is what I did to solve the problem.

  1. Fork the fork (you can find my version here)
  2. Update the version number of the project. In this case I opened pyproject.toml and put version="0.0.9" instead of version="0".
  3. Create a new tag with the name "0.0.9". This process was kind of messy in github, I ended up create a branch, a release and then finally a tag. I am sure there is a more elegant way to do it.
  4. Put the following line in my requirements.txt: git+https://github.com/mrdeveloperdude/[email protected]#egg=sqlmodel. Please note there is no "-e" as this would simply mean I want an editable package which is not the case.
  5. run pip install -r requirements.txt again
  6. Profit!

It did complain a lot but in the end those complaints were just words in my terminal. My app got Pydantic 2 support and all was well.

Final words: I think this is ugly, unecessary, hacky and inelegant in ways that just.. it makes my skin crawl frankly. I hope there is a hidden option somewhere that would allow one to just invent a version and apply that to a git repo without having to fork it.

Systematic answered 30/9, 2023 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.