From virtualenv, pip freeze > requirements.txt give TONES of garbage! How to trim it out?

后端 未结 3 1587
时光取名叫无心
时光取名叫无心 2020-12-23 17:14

I\'m following this tutorial: http://devcenter.heroku.com/articles/django

At some point I\'m suposed to do:

pip freeze > requirements.txt
<         


        
3条回答
  •  半阙折子戏
    2020-12-23 17:34

    That is one thing that has bugged me too quite a bit. This happens when you create a virtualenv without the --no-site-packages flag.

    There are a couple of things you can do:

    1. Create virtualenv with the --no-site-packages flag.
    2. When installing apps, dont run pip install directly, instead, add the library to your requirements.txt first, and then install the requirements. This is slower but makes sure your requirements are updated.
    3. Manually delete libraries you dont need. A rule of thumb i follow for this is to add whatever is there in my INSTALLED_APPS, and database adapters. Most other required libraries will get installed automatically because of dependencies. I know its silly, but this is what I usually end up doing.

    -- Edit --

    I've since written a couple of scripts to help manage this. The first runs pip freeze and adds the found library to a provided requirements file, the other, runs pip install, and then adds it to the requirements file.

    function pipa() {
        # Adds package to requirements file.
        # Usage: pipa  
        package_name=$1
        requirements_file=$2
        if [[ -z $requirements_file ]]
        then
            requirements_file='./requirements.txt'
        fi
        package_string=`pip freeze | grep -i $package_name`
        current_requirements=`cat $requirements_file`
        echo "$current_requirements\n$package_string" | LANG=C sort | uniq > $requirements_file
    }
    
    function pipia() {
        # Installs package and adds to requirements file.
        # Usage: pipia  
        package_name=$1
        requirements_file=$2
        if [[ -z $requirements_file ]]
        then
            requirements_file='./requirements.txt'
        fi
        pip install $package_name
        pipa $package_name $requirements_file
    }
    

提交回复
热议问题