62

I'm searching for a way to install a package with pip, and write that package's version information to my project's requirements.txt file. For those familiar with npm, it's essentially what npm install --save does.

Using pip freeze > requirements.txt works great, but I've found that I forget to run this, or I can accidentally include unused packages that I'd installed for testing but decided not to use.

So the following psuedocode:

$ pip install nose2 --save

Would result in a requirements.txt file with:

nose2==0.4.7

I guess I could munge the output of save to grab the version numbers, but I am hoping there is an easier way.

3
  • 1
    write a bash and pass a command line arg echo $1 >> requirements.txt; pip install $1 Commented Nov 15, 2013 at 16:46
  • 5
    That isn't a complete solution at all, because it doesn't resolve the version correctly like pip freeze would do Commented Apr 16, 2015 at 15:14
  • Switch to Pipfile or Poetry if you need this Commented Nov 6, 2024 at 1:19

3 Answers 3

25

To get the version information, you can actually use pip freeze selectively after install. Here is a function that should do what you are asking for:

pip_install_save() {
    package_name=$1
    requirements_file=$2
    if [[ -z $requirements_file ]]
    then
        requirements_file='./requirements.txt'
    fi
    pip install $package_name && pip freeze | grep -i $package_name >> $requirements_file
}

Note the -i to the grep command. Pip isn't case sensitive with package names, so you will probably want that.

Sign up to request clarification or add additional context in comments.

4 Comments

That grep statement has the potential to duplicate items in your requirements file. if django-example-package is already in your requirements and you install django. The requirements file will now have django-example-package added to the end of it.
Sure. That edge case can be handled with a little more care with the regular expression. I'm sure there are other ways that function can be broken, but it's a good start, I think.
@dusktreader I've used ^$package_name== that and it looks good. The edit is still waiting for pree-review though.
in which extention should it be saved??
8

I've written the following bash function which I use;

function pip-save() {
    for pkg in $@; do
        pip install "$pkg" && {
            name="$(pip show "$pkg" | grep Name: | awk '{print $2}')";
            version="$(pip show "$pkg" | grep Version: | awk '{print $2}')";
            echo "${name}==${version}" >> requirements.txt;
        }
    done
}

This saves the canonical package name to requirements, as well as the version installed. Example usage;

$ pip-save channels asgi_redis
# will save the following to requirements.txt (as of writing):
# ---
# channels==1.0.1
# asgi-redis==1.0.0
# ---
# note how asgi_redis is translated to its canonical name `asgi-redis`

6 Comments

Please don't post the same answer to multiple questions! If the questions really have the exact same answer, one of them is a duplicate and should be flagged for closure as such.
@miken32 I disagree. I think this is the best answer so far
@AlexCory I didn't say there was anything wrong with it. Just that copying and pasting answers between questions is not good practice.
@miken32 ohhhhh. I see what you're saying. Well, I see where you're coming from, but I didn't go look at a bunch of questions to find this and wasn't planning to, so I would never have seen it had it not been here. Just something to think about. (unless you're saying he stole the answer from another post, then that's different)
@AlexCory questions and answers are regularly reviewed by users with enough rep. In this case the duplicate answer was subsequently deleted. Ideally, questions are flagged as duplicates so that there is a link from one question to the other.
|
6

Just add smth like

function pips() {
    echo $'\n'$1 >> requirements.txt; pip install $1
}

into your .bashrc or .bash_profile and use pips command to install package and save it's name into requirements.txt example:

pips django-waffle

based on Akash Kothawale comment :)

2 Comments

This solves adding the name of the package to requirements.txt, but it leaves the version part of my question out :)
That isn't a complete solution because it doesn't resolve the version correctly like pip freeze would do. Also adds to requirements.txt before installing first which is bad if pip install fails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.