Python Project Dependencies: Difference between revisions
No edit summary Tag: wikieditor |
|||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
[[Category:Python]] [[Category:Web Development]] | [[Category:Python]] [[Category:Web Development]] | ||
== Workflow overview == | |||
Add a new pip dependencies to `requirements-to-freeze.txt`. | |||
<syntaxhighlight lang="bash"> | |||
# Install the dependencies with: | |||
$ pip install -r requirements-to-freeze.txt --upgrade | |||
# Save the specific dependency versions with: | |||
$ pip freeze > requirements.txt | |||
</syntaxhighlight> | |||
See [https://www.kennethreitz.org/essays/a-better-pip-workflow A Better Pip Workflow] for more details. | |||
== requirements.txt == | == requirements.txt == | ||
First, for every project create and load a [[Virtual Environments with Python|virtual environment]] specific to the project before installing dependencies. | |||
Project dependencies are stored in a file named `requirements.txt` in the top level of the project, e.g.: <ref>[https://www.fullstackpython.com/application-dependencies.html Application Dependencies], Full Python Stack</ref> | Project dependencies are stored in a file named `requirements.txt` in the top level of the project, e.g.: <ref>[https://www.fullstackpython.com/application-dependencies.html Application Dependencies], Full Python Stack</ref> | ||
< | `requirements.txt` defines ''concrete'' dependencies for non-redistributable applications. It defines the dependencies between packages and the source for those packages, especially if they don't come from PyPi. | ||
An example `requirements.txt`: | |||
<syntaxhighlight lang="txt"> | |||
django==1.6 | django==1.6 | ||
bpython==0.12 | bpython==0.12 | ||
| Line 16: | Line 37: | ||
django-oauth2-provider==0.2.4 | django-oauth2-provider==0.2.4 | ||
djangorestframework==2.3.1 | djangorestframework==2.3.1 | ||
</ | </syntaxhighlight> | ||
=== Saving dependencies === | |||
Anytime a package is installed with `pip`, it must be added to `requirements.txt`. Or better yet, add the package to `requirements.txt` and use the file to install the new package and verify the project's environment. | Anytime a package is installed with `pip`, it must be added to `requirements.txt`. Or better yet, add the package to `requirements.txt` and use the file to install the new package and verify the project's environment. | ||
A quick and reliable way to track all the packages currently installed with pip is with | |||
`requirements.txt` | <syntaxhighlight lang="bash"> | ||
$ pip freeze > requirements.txt | |||
</syntaxhighlight> | |||
This saves the packages along with their current version. This means that installing from `requirements.txt` in the future might mean installing older versions of packages. [https://www.kennethreitz.org/essays/a-better-pip-workflow This article] has a recommendation for tracking both the top-level packages and all their dependencies. <ref>[https://www.kennethreitz.org/essays/a-better-pip-workflow A Better Pip Workflow] - Kenneth Reitz</ref> | |||
=== Installing dependencies === | |||
Dependencies in `requirements.txt` can be installed with: | |||
<syntaxhighlight leng="bash"> | <syntaxhighlight leng="bash"> | ||
| Line 62: | Line 91: | ||
$ python setup.py install | $ python setup.py install | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Alternate workflow == | |||
Save the top-level dependencies in one file, then save the specific dependencies in `requirements.txt`. | |||
See [https://kenreitz.org/essays/a-better-pip-workflow A Better Pip Workflow] for details. | |||
But basically, on the development server create a file named `requirements-to-freeze.txt`. In that file list the top-level packages used by the project. | |||
<pre> | |||
$ pip install -r requirements-to-freeze.txt --upgrade | |||
$ pip freeze > requirements.txt | |||
</pre> | |||
== Notes == | == Notes == | ||
Latest revision as of 16:11, 2 July 2023
Workflow overview[edit]
Add a new pip dependencies to requirements-to-freeze.txt.
# Install the dependencies with: $ pip install -r requirements-to-freeze.txt --upgrade # Save the specific dependency versions with: $ pip freeze > requirements.txt
See A Better Pip Workflow for more details.
requirements.txt[edit]
First, for every project create and load a virtual environment specific to the project before installing dependencies.
Project dependencies are stored in a file named requirements.txt in the top level of the project, e.g.: [1]
requirements.txt defines concrete dependencies for non-redistributable applications. It defines the dependencies between packages and the source for those packages, especially if they don't come from PyPi.
An example requirements.txt:
django==1.6 bpython==0.12 django-braces==0.2.1 django-model-utils==1.1.0 logutils==0.3.3 South==0.7.6 requests==1.2.0 stripe==1.9.1 dj-database-url==0.2.1 django-oauth2-provider==0.2.4 djangorestframework==2.3.1
Saving dependencies[edit]
Anytime a package is installed with pip, it must be added to requirements.txt. Or better yet, add the package to requirements.txt and use the file to install the new package and verify the project's environment.
A quick and reliable way to track all the packages currently installed with pip is with
$ pip freeze > requirements.txt
This saves the packages along with their current version. This means that installing from requirements.txt in the future might mean installing older versions of packages. This article has a recommendation for tracking both the top-level packages and all their dependencies. [2]
Installing dependencies[edit]
Dependencies in requirements.txt can be installed with:
$ pip install -r requirements.txt
setup.py[edit]
In addition to requirements.txt, setup.py is another file used to check for dependencies when installing a package. [3]
setup.py defines abstract dependencies for redistributable packages. The specific source for those dependencies should be defined elsewhere, e.g. requirements.txt.
Given a directiory containing a setup.py file, a requirements.txt file can be written that will install all dependencies found in setup.py from PyPi:
--index-url https://pypi.python.org/simple -e .
If any dependencies should come from another source, requirements.txt can be modified similar to this:
--index-url https://pypi.python.org/simple -e https://github.com/foo/bar.git#egg=bar -e .
pip will first install the bar library from GitHub, then move on to install all the dependencies found in setup.py. Since the bar library has already been installed at this point, it will be skipped over when installing libraries from PyPi.
Installing packages defined in setup.py[edit]
Command to install the project dependencies defined in setup.py: [4] [5]
$ python setup.py install
Alternate workflow[edit]
Save the top-level dependencies in one file, then save the specific dependencies in requirements.txt.
See A Better Pip Workflow for details.
But basically, on the development server create a file named requirements-to-freeze.txt. In that file list the top-level packages used by the project.
$ pip install -r requirements-to-freeze.txt --upgrade $ pip freeze > requirements.txt
Notes[edit]
See also[edit]
References[edit]
- ↑ Application Dependencies, Full Python Stack
- ↑ A Better Pip Workflow - Kenneth Reitz
- ↑ setup.py vs requirements.txt, Donald Stufft
- ↑ What is setup.py - Stackoverflow
- ↑ Installing Python Modules - Python Software Foundation