【问题标题】:From virtualenv, pip freeze > requirements.txt give TONES of garbage! How to trim it out?从 virtualenv 中,pip freeze > requirements.txt 给出了一堆垃圾!如何修剪它?
【发布时间】:2012-03-15 16:51:22
【问题描述】:

我正在学习本教程: http://devcenter.heroku.com/articles/django

在某些时候我应该这样做:

pip freeze > requirements.txt

(Ofc. from virtualenv 创建的 python 实例)

我明白了:

(venv)przemoli@ubuntu:~/Programowanie/hellodjango$ cat requirements.txt 
BeautifulSoup==3.2.0
Brlapi==0.5.5
CherryPy==3.1.2
ClientForm==0.2.10
Django==1.3
GnuPGInterface==0.3.2
PAM==0.4.2
PIL==1.1.7
Routes==1.12.3
Twisted-Core==11.0.0
Twisted-Names==11.0.0
Twisted-Web==11.0.0
WebOb==1.0.8
adium-theme-ubuntu==0.3.1
apt-xapian-index==0.44
apturl==0.5.1ubuntu1
chardet==2.0.1
command-not-found==0.2.44
configglue==1.0
cssutils==0.9.8a1
defer==1.0.2
distribute==0.6.19
django-tagging==0.3.1
dnspython==1.9.4
duplicity==0.6.15
gnome-app-install==0.4.7-nmu1ubuntu2
httplib2==0.7.2
jockey==0.9.4
keyring==0.6.2
launchpadlib==1.9.8
lazr.restfulclient==0.11.2
lazr.uri==1.0.2
louis==2.3.0
lxml==2.3
mechanize==0.1.11
nvidia-common==0.0.0
oauth==1.0.1
onboard==0.96.1
oneconf==0.2.6.7
papyon==0.5.5
pexpect==2.3
piston-mini-client==0.6
protobuf==2.4.0a
psycopg2==2.4.4
pyOpenSSL==0.12
pycrypto==2.3
pycups==1.9.59
pycurl==7.19.0
pyinotify==0.9.1
pyparsing==1.5.2
pyserial==2.5
pysmbc==1.0.10
python-apt==0.8.0ubuntu9
python-dateutil==1.4.1
python-debian==0.1.20ubuntu2
python-virtkey==0.60.0
pyxdg==0.19
sessioninstaller==0.0.0
simplejson==2.1.6
system-service==0.1.6
ubuntu-sso-client==1.4.0
ubuntuone-couch==0.3.0
ubuntuone-installer==2.0.0
ubuntuone-storage-protocol==2.0.0
ufw==0.30.1-2ubuntu1
unattended-upgrades==0.1
usb-creator==0.2.23
virtualenv==1.6.4
wadllib==1.2.0
wsgiref==0.1.2
xdiagnose==1.1
xkit==0.0.0
zope.interface==3.6.1

在 heroku 上部署时,它在Brlapi 失败.....

我在 ubuntu 上的主要 python 安装中看到了很多东西。这很糟糕,因为 Ubuntu 本身使用 python 做很多事情(ubuntu-one、usb-creator 等)。

我在 heroku 上不需要它们!我只需要 Django、psycopg2 和它们的依赖项。 我什至不知道是 pip 的错,还是 virutalenv 的错。 (如果您想知道我的设置,请查看上面的链接,我将其复制到终端中)

【问题讨论】:

    标签: django deployment heroku virtualenv pip


    【解决方案1】:

    简单地说,
    pip3 freeze requirements.txt
    那么如果你想安装所有
    pip3 install -r requirements.txt

    【讨论】:

      【解决方案2】:

      你可以使用:

      pip freeze --local > 要求.txt

      因此,requirements.txt 中仅列出了您的 virtualenv 中本地安装的包,而不是全局安装的包。

      【讨论】:

        【解决方案3】:

        pipreqs 解决了这个问题。它生成项目级 requirements.txt 文件。

        • 安装 pipreqs:pip install pipreqs
        • 生成项目级需求.txt文件:pipreqs /path/to/your/project/ 需求文件将保存在 /path/to/your/project/requirements.txt

        【讨论】:

        • 遗憾的是它不起作用,它创建了一个需求,但仍然缺少一些模块
        • @Roman 如果你使用 virtualenv 来运行你的项目,请确保你使用属于 virtualenv 安装的 pip pipreqs
        • 似乎按我的预期工作,建议作为快速修复。
        • 我似乎无法让它工作。 "没有名为 pipreqs.__main__ 的模块;'pipreqs' 是一个包,不能直接执行"
        • @lmblanes 检查您的点子是否过时
        【解决方案4】:

        这是一件让我非常烦恼的事情。当您创建一个没有 --no-site-packages 标志的 virtualenv 时会发生这种情况。

        你可以做几件事:

        1. 使用 --no-site-packages 标志创建 virtualenv。
        2. 安装应用程序时,不要直接运行pip install <name>,而是先将库添加到您的requirements.txt,然后安装需求。这速度较慢,但​​可确保更新您的要求。
        3. 手动删除不需要的库。我遵循的经验法则是添加我的INSTALLED_APPS 和数据库适配器中的任何内容。由于依赖关系,大多数其他必需的库将自动安装。我知道这很愚蠢,但这是我通常最终会做的事情。

        -- 编辑--

        我已经写了一个couple of scripts 来帮助管理这个。第一个运行 pip freeze 并将找到的库添加到提供的需求文件中,另一个运行 pip install,然后将其添加到需求文件中。

        function pipa() {
            # Adds package to requirements file.
            # Usage: pipa <package> <path to requirements file>
            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> <path to requirements file>
            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
        }
        

        【讨论】:

        • 注意:从 v. 1.7 开始,virtualenv 默认采用--no-site-packages,因此不需要您指定该选项。这是默认设置。链接:pypi.python.org/pypi/virtualenv#changes-news
        • 谢谢! --no-site-packages 工作正常! (尽管我仍然很困惑为什么 heroku 工作人员没有在他们的文档中提到它)。 (是的,我确实使用了 Ubuntu 11.10 提供的包含 virtualenv 1.6.4 的软件包 :( :( :( )
        • 如果您使用的是最新版本的 virtualenv,则不再需要 --no-site-packages。我强烈建议不要依赖 aptitude 的 python 模块 :)
        • 你也可以pip freeze --local &gt; requirements.txt 这将只输出安装到你的虚拟环境中的包,而不列出所有依赖项(包本身,处理这些。)
        【解决方案5】:

        如果您非常关心requirements.txt 的清洁度,您不仅应该使用前面提到的--no-site-packages 选项,还应该考虑不要将pip freeze 的输出直接通过管道传输到requirements.txt。这样做的原因是,在执行pip freeze 时,不仅会显示您指定的包,还会显示这些包安装的依赖项!没有必要将它们全部保存在您的 requirements.txt 中,因为它们将与需要它们的软件包一起自动安装...... 因此,如果您将一个新包添加到您的 virtualenv,您可能应该将此包的行添加到您的 requirements.txt...

        看这个例子:

        (demo)[~]$ pip freeze
        distribute==0.6.19
        wsgiref==0.1.2
        (demo)[~]$ pip install django-blog-zinnia
        Downloading/unpacking django-blog-zinnia
          Downloading django-blog-zinnia-0.9.tar.gz (523Kb): 523Kb downloaded
          Running setup.py egg_info for package django-blog-zinnia
        
            no previously-included directories found matching 'docs/api'
            no previously-included directories found matching 'docs/build'
            no previously-included directories found matching 'docs/coverage'
            no previously-included directories found matching 'zinnia/media/zinnia/css/.sass-cache'
        Downloading/unpacking BeautifulSoup>=3.2.0 (from django-blog-zinnia)
          Downloading BeautifulSoup-3.2.1.tar.gz
          Running setup.py egg_info for package BeautifulSoup
        
          # truncated as it installs some more dependencies
        Successfully installed django-blog-zinnia BeautifulSoup django-mptt django-tagging django-xmlrpc pyparsing
        Cleaning up...
        (demo)[~]$ pip freeze
        BeautifulSoup==3.2.1
        distribute==0.6.19
        django-blog-zinnia==0.9
        django-mptt==0.5.2
        django-tagging==0.3.1
        django-xmlrpc==0.1.3
        pyparsing==1.5.6
        wsgiref==0.1.2
        

        (虽然我可能应该提到,在大多数情况下,在那里拥有这些依赖项不会有什么坏处,只是你的文件会增长并且变得更难维护。)

        【讨论】:

        • 不!我只是想要快速的解决方案,将我的操作系统相关的 python 库与应该安装在部署机器上的 django 开发库分开! (恕我直言,应该更改 pip freeze 的默认行为...)
        • 我认为您绝对应该在您的 requirements.txt 中保留所有间接依赖项,以及它们的版本号。这样你的构建是可重复的。我过去常常将我的直接需求放在文件的顶部,而将我的间接(“传递”)依赖关系放在底部。现在我只是将它们全部保存在一个文件中,使用 pip freeze 生成信息,但使用 cmets,并按字母顺序排列。
        猜你喜欢
        • 2019-08-08
        • 1970-01-01
        • 2018-06-22
        • 1970-01-01
        • 1970-01-01
        • 2022-01-12
        • 1970-01-01
        • 1970-01-01
        • 2017-03-23
        相关资源
        最近更新 更多