简体   繁体   English

在 GitLab 中创建 requirements.txt

[英]Creating requirements.txt in GitLab

Probably a silly question, but I am trying to set up a project in GitLab that is going to be used for deployment of an ML model, for which I will use FastAPI.可能是一个愚蠢的问题,但我正在尝试在 GitLab 中设置一个项目,该项目将用于部署 ML model,为此我将使用 FastAPI。 I'm very new to this and will try to provide as much info as possible.我对此很陌生,并将尝试提供尽可能多的信息。

I created the project in GitLab, which right now only contains a README.md file.我在 GitLab 中创建了项目,该项目现在只包含一个 README.md 文件。 The actual Python code is stored in a folder on my computer ("MyProject"), which contains two folders, each of which containing some data, .py scripts and a notebook.实际的 Python 代码存储在我计算机上的一个文件夹(“MyProject”)中,其中包含两个文件夹,每个文件夹都包含一些数据、.py 脚本和一个笔记本。

To set up requirements.txt , I tried to create a virtual environment in Windows.为了设置requirements.txt ,我尝试在 Windows 中创建一个虚拟环境。 Now, when I open the "MyProject" folder, it contains those two folders with code and the virtual enviroment, which also contains Lib, Scripts, pyvenv.cfg .现在,当我打开“MyProject”文件夹时,它包含这两个带有代码的文件夹和虚拟环境,其中还包含Lib, Scripts, pyvenv.cfg The latter contains:后者包含:

home = c:\users\me\anaconda3
implementation = CPython
version_info = 3.8.5.final.0
virtualenv = 20.10.0
include-system-site-packages = false
base-prefix = c:\users\me\anaconda3
base-exec-prefix = c:\users\me\anaconda3
base-executable = c:\users\me\anaconda3\python.exe

I also cloned the GitLab repo, but on my computer it is saved somewhere else (in c:\users\me ).我还克隆了 GitLab 存储库,但在我的计算机上它保存在其他地方(在c:\users\me中)。 I know that I need to do:我知道我需要这样做:

pip install -r
requirements.txt

But I don't understand how to actually add this requirements file.但我不明白如何实际添加这个需求文件。 All of the packages and libraries that I needed for my ML model were installed a long time ago with anaconda, before I created this virtual environment.在我创建这个虚拟环境之前,我的 ML model 所需的所有包和库都是很久以前用 anaconda 安装的。 Have I done anything wrong?我做错什么了吗?

I think you mixed up some things.我认为你混淆了一些东西。 GitLab uses Git for version control of your files (your code). GitLab 使用 Git 对您的文件(您的代码)进行版本控制。 Therefore your repository should contain the files with your code.因此,您的存储库应包含带有您的代码的文件。 You can just put the files of your folder "MyProject" into the folder, where you cloned the repository to.您可以将文件夹“MyProject”的文件放入您将存储库克隆到的文件夹中。 Also add the requirements.txt the readme-file and so on.还要添加 requirements.txt 自述文件等。

The virtual environment is used to keep your system installation of Python clean and only have the necessary packages installed for each project.虚拟环境用于保持 Python 的系统安装干净,并且只为每个项目安装必要的软件包。 Among other things to avoid package requirement conflicts.除其他事项外,要避免 package 要求冲突。 The usage of an requirements.txt file is independet of the virtual environment, even if it is a sensible combination. requirements.txt 文件的使用与虚拟环境无关,即使它是一个明智的组合。

In general this means, your requirements.txt is always shared together with your code, because it lays within the same repository.一般来说,这意味着您的 requirements.txt 始终与您的代码共享,因为它位于同一个存储库中。 When someone clones the repository, he can use the requirements.txt to install all the dependencies to his venv (or somewherer else) and then run your code without the nedd to install further python packages.当有人克隆存储库时,他可以使用 requirements.txt 将所有依赖项安装到他的 venv(或其他地方),然后在没有 nedd 的情况下运行您的代码以安装更多 python 包。

Your requirements.txt file has to contain columns, which look like this: numpy==1.21.4 .您的 requirements.txt 文件必须包含如下所示的列: numpy==1.21.4 Then you have to activate the environment with <your path to the venv folder>\venv\Scripts\activate and use python -m pip install -r requirements.txt to install the packages listed in your requirements.txt.然后您必须使用<your path to the venv folder>\venv\Scripts\activate环境并使用python -m pip install -r requirements.txt来安装您的 requirements.txt 中列出的包。

You can create requirements.txt using pip freeze > requirements.txt and add it to your repo.您可以使用pip freeze > requirements.txt创建requirements.txt并将其添加到您的存储库中。 This will generate list of your installed packages and exact versions you have.这将生成您已安装的软件包和您拥有的确切版本的列表。

https://pip.pypa.io/en/stable/cli/pip_freeze/ https://pip.pypa.io/en/stable/cli/pip_freeze/

Simple solution would be pip freeze > requirements.txt but this command will add all the packages present in your enviroment which may not be used in your project.简单的解决方案是pip freeze > requirements.txt但此命令将添加您的环境中存在的所有包,这些包可能不会在您的项目中使用。 In my daily job, I use this https://pypi.org/project/pipreqs/ .在我的日常工作中,我使用这个https://pypi.org/project/pipreqs/ you can install it and run pipreqs --force in your project folder.您可以安装它并在您的项目文件夹中运行pipreqs --force This will add packages to requirements.txt which are used in your project.这会将包添加到您项目中使用的 requirements.txt 中。

how to actually add this requirements file如何实际添加此需求文件

You create the file yourself.您自己创建文件。 For every library that you use, add a line in requirements.txt with the name of the library.对于您使用的每个库,在requirements.txt中添加一行,其中包含库的名称。 Also see documentation https://pip.pypa.io/en/stable/reference/requirements-file-format/另请参阅文档https://pip.pypa.io/en/stable/reference/requirements-file-format/

After creating the file, commit and push it to the git repository.创建文件后, commit并将其push送到 git 存储库。

All of the packages and libraries that I needed for my ML model were installed a long time ago with anaconda我的 ML model 所需的所有包和库都是很久以前用 anaconda 安装的

Gitlab-CI with docker executor starts with a fresh environment.带有 docker 执行器的 Gitlab-CI 从一个全新的环境开始。 You have to repeat all installation steps that you did on your workstation inside the docker environment.您必须重复docker 环境中的工作站上执行的所有安装步骤。 You can run docker instance of the container locally for testing.您可以在本地运行容器的 docker 实例进行测试。 Consult gitalb-ci and docker documentations.查阅 gitalb-ci 和 docker 文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM