What is a dependency specification
Project dependencies can be specified in various forms, depending on the type of dependency and the optional constraints that might be required to install the project
Version constraint
^ constraints
Write specifications | Allowed version range |
---|---|
^ 1.2.3 | > = 1.2.3 < 2.0.0 |
^ 1.2 | > = 1.2.0 < 2.0.0 |
^ 1 | > = 1.0.0 < 2.0.0 |
^ 0.2.3 | > = 0.2.3 < 0.3.0 |
^ 0.0.3 | > = 0.0.3 < 0.0.4 |
^ 0.0 | > = 0.0.0 < 0.1.0 from |
^ 0 | > = 0.0.0 < 1.0.0 |
- If the leftmost number is not 0, the leftmost number is the major version number. For example, ^2.13.0 can be 2.14.0, but cannot be 3.0.0, because the major version number has changed
- If the first digit on the left is 0, the second digit on the left is the main version number. For example, ^0.1.0 can be 0.1.1 or 0.1.19, but cannot be 0.2.0 because the main version number has changed
~ constraints
Write specifications | Allowed version range |
---|---|
~ 1.2.3 | > = 1.2.3 < 1.3.0 |
~ 1.2 | > = 1.2.0 < 1.3.0 |
~ 1 | > = 1.0.0 < 2.0.0 |
Similar to ^ above, but this is a minor version, based on the second number. Finally, if your time is not very tight, and want to quickly improve, the most important thing is not afraid of hardship, I suggest you can price @762459510, that is really very good, many people progress quickly, need you not afraid of hardship oh! You can go to add a look at ~
* constraint
It’s kind of like a universal card, you can write it anywhere, right
Write specifications | Allowed version range |
---|---|
* | > = 0.0.0 |
1. * | > = 1.0.0 < 2.0.0 |
1.2. * | > = 1.2.0 < 1.3.0 |
Comparison operators
The normal > and < symbols
>= 1.2.0
> 1
< 2
!= 1.2.3
Copy the code
A defined version number or scope
Bonus: Private Reply [01] get a free introduction to Python tutorial video
>= 1.2, < 1.5Copy the code
Git is dependent on
You can specify a Git repository address for dependencies
[tool.poetry.dependencies]
requests = { git = "https://github.com/requests/requests.git" }
Copy the code
The default is to pull the Master branch of the Git repository
You can also specify branch, commit hash, or tag
[tool.poetry.dependencies] # Get the latest revision on the branch named "next" requests = { git = "https://github.com/kennethreitz/requests.git", branch = "next" } # Get a revision by its commit hash flask = { git = "https://github.com/pallets/flask.git", Rev = "38 eb5d3b"} # Get a revision by its tag numpy = {git = "https://github.com/numpy/numpy.git", tag = "v0.13.2}"Copy the code
Path dependence
If the dependency is in a local directory, you can use path
[tool.poetry.dependencies] # directory my-package = { path = ".. /my-package/", develop = false } # file my-package = { path = ".. / my - package/dist/my - package - 0.1.0 from. Tar. Gz "}Copy the code
Url to rely on
If you rely on files from a remote repository, use urls
[tool.poetry. Dependencies] # directory my-package = {url = "https://example.com/my-package-0.1.0.tar.gz"}Copy the code
You can add urls via poetry Add
Poetry add https://example.com/my-package-0.1.0.tar.gzCopy the code
Python restricts dependencies
Specifies that dependencies should only be installed with a particular Python version
[tool.poetry. Dependencies] pathlib2 = {version = "^2.2", python = "~2.7"}Copy the code
[tool. Poetry. Dependencies] pathlib2 = {version = "2.2" ^, python = "~ 2.7 | | ^ 3.2"}Copy the code
Multiple restrictions
Hypothetical dependency package
Python 2.7 to Python 2.9 is compatible only with versions 1.9 or smaller
Versions greater than 2.0 are only compatible with Python 3.4 +
[tool. Poetry. Dependencies] foo = [{version = "< = 1.9," python = "^ 2.7"}, {version = "^ 2.0," python = "^ 3.4"}]Copy the code
Use environment Restriction
Feel less use, not to expand the detailed explanation
[tool.poetry. Dependencies] pathlib2 = {version = "^2.2", Markers = "python_version ~= '2.7' or sys_platform == 'win32'"}Copy the code
Markers of official document: www.python.org/dev/peps/pe…
Extensions rely on canonical syntax
When a dependency requires many attributes, the readability is poor, as shown below
[tool.poetry. Dev-dependencies] black = {version = "19.10b0", allow-prereleases = true, python = "^3.6", markers = "platform_python_implementation == 'CPython'"}Copy the code
Use the new syntax format
[tool.poetry. Dev-dependencies. Black] version = "19.10b0" allow-prereleases = true python = "^3.6" markers = "platform_python_implementation == 'CPython'"Copy the code
The constraints of dependencies are exactly the same, but become a row of constraint attributes, which is more readable. Finally, if your time is not very tight, and want to quickly improve, the most important thing is not afraid of hardship, I suggest you can price @762459510, that is really very good, many people progress quickly, need you not afraid of hardship oh! You can go to add a look at ~
This is the Python dependency management and packaging tool Poetry dependency specification. For more information about the Python tool Poetry dependency specification, check out other related articles.
If the article is helpful to you, please give me a thumbs up!
\