在阅读Kenneth Retiz写的ptyhon最佳实践文章中关于如何构建一个好的目录结构的时候,发现有这样一个问题,可以同时存在setup.py
文件以及requirements.txt
文件,想请问一下这两者之间有什么区别与联系。
例如有下面这样的一个flask
应用目录结构,setup.py与requirements.txt两个文件同时存在是否有必要?
├── app ├── docs ├── test ├── config.py ├── manage.py ├── requirements.txt ├── setup.py ├── Makefile ├── README.md └── LICENSE
Whether this question is necessary actually depends on the nature of the project. Generally speaking, there will be both, and it is best to have both.
setup.py
This file is used to manage modules. You can package your project as a module and upload it to pypi. You can also put the project as a module into the python system loading module directory.requiremens.txt
This file lists the non-project dependencies that your project references. Other than that, it has no other function.Therefore, it can be seen that there is no conflict between these two projects, and there is a great need to coexist.
It is essential if your project needs to be saved as a system module or can be installed as a system command
setup.py
If your project can be run directly, then
requirements.txt
is also essential.If you need both functions, then both files must be included.
Do you need both functions?
There must be one, and there are many more. For example, Flask supports two methods:
Start as a system command. A new Click package has been added in Flask 0.11.1, which allows you to run it as a command after setup
Direct operation, traditional
Flask
启动方式一般就是app.run()
或者python manage.py runserver
Hope it’s helpful to the questioner.
Added:
In fact,
during setup, because there is no need to associate this, the author has already written it in .setup.py
里面也会有依赖的列表,所以,在 setup 的时候无需担心会和requirements.txt
关联,因为不需要关联这个,作者已经在setup.py
will also have a list of dependencies, so there is no need to worry about being associated withI saw that requirements.txt is a must. It declares which third-party libraries are introduced into your project. As a flask web application setup.py doesn't seem to make sense. . .
setup.py
是安装你自己写的模块,一般依靠执行sudo python setup.py install
,requirements.txt
是指明这个模块所依赖的模块,一般执行sudo pip install -r requirements.txt
To install dependent modules.