Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
814 views
in Technique[技术] by (71.8m points)

pip - Simple dependency management for a Python project

I am coming from Java background and completely new at Python.

Now I have got a small project with a few Python files that contain a few imports. I know I does not have the imported dependencies installed on my computer, so I try to figure out the required dependencies and run pip to install them.

I would like to do it differently. I would prefer to have the dependencies listed in a single file and install them automatically during the build process.

Does it make sense ? If it does I have a few questions:

  • How to list the project dependencies required to install by pip ?
  • How to run pip to install the dependencies from the list ?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A common way to manage dependencies for a python project is via a file in root of the project named "requirements.txt". An easy way to make this is:

  1. Setup a python virtualenv for your project
  2. Manually install the required modules via pip
  3. Execute pip freeze > requirements.txt to generate the requirements file

You can then install all the dependencies in other locations using pip install -r requirements.txt.

If you want dependencies to be installed automatically when other people pip install your package, you can use install_requires() in your setup.py.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...