Hướng dẫn python setup py install test dependencies - python setup py install test phụ thuộc

Dung dịch

Tox 2.6 đã giới thiệu tùy chọn

[testenv]
...
extras = tests
...
8. Điều này sẽ cài đặt
[testenv]
...
extras = tests
...
8 từ SDist mà nó vừa xây dựng, chỉ cho SDist đó và tại thời điểm đó, nó đang thực hiện cài đặt SDIST bình thường.

setup.py trông giống như:

setuptools.setup(
    ...
    extras_require={
        'tests': ['pytest>=3.7.0', 'more_packages'],
    },
    ...
 )

Tox.ini trông giống như:

[testenv]
...
extras = tests
...

Mối quan tâm

Các phương pháp khác có thể nhận được kết quả tương tự nhưng đưa ra rủi ro không cần thiết và giới hạn tính hữu ích của các tính năng khác:

        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
0 là một chút hack. Lĩnh vực dành cho các gói nhu cầu môi trường. Nếu setup.py install_requires tham chiếu một gói khác mà bạn phát triển, bạn có thể sử dụng nó để kéo phiên bản trước khi phát hành của nó. Như được hiển thị, nó sẽ cài đặt toàn bộ gói của bạn từ thư mục làm việc của bạn (bất kỳ trạng thái nào đang ở!) Chỉ để có được trong danh sách các gói trong
        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
1.
        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
2 sẽ chạy tiếp theo, cài đặt sdist mới được đúc của bạn. Nói tóm lại, các vấn đề với sdist có thể được che dấu vì bạn đã cài đặt từ bản sao làm việc của mình.

Chỉnh sửa

        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
2 là quá mức cần thiết. Nó sẽ ghi đè các mục được cài đặt thông qua
        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
4. (Một lần nữa có thể bạn đã sử dụng nó để cài đặt một phiên bản cụ thể của gói).

        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
5 được sử dụng khi
        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
6 được chạy. Tox khuyên bạn nên tránh
        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
6 để bạn có thể bỏ qua tất cả cùng nhau.

Tôi đã tìm thấy vấn đề này trong CCDProc, nhưng tôi báo cáo ở đây vì nó dường như là một vấn đề của những người giúp đỡ Astropy.

CCDProc yêu cầu mô-đun pytest-capturelog và chỉ định rằng trong

        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
5 của nó trong setup.py. .
( https://github.com/astropy/ccdproc/blob/master/setup.py#L98 )
I don't have pytest-capturelog installed on my machine and the corresponding tests fail with

        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    

Thất bại của luận án biến mất sau khi cài đặt thủ công pytest-capturelog. Do đó, có vẻ như

        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
6 không cài đặt các phụ thuộc cần thiết hoặc nó cài đặt chúng ở một vị trí mà chúng không được tìm thấy. (Sự hiểu biết của tôi là các phụ thuộc
        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
5 chỉ được cài đặt trong thư mục tạm thời, nhưng tôi có thể sai.)
Thus, it seems that either
        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
6 does not install the required dependencies or it installs them in a location where they are not found.
(My understanding is that
        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
5 dependencies are installed in a temporary directory only, but I might be wrong.)

Có hai cách giải quyết rõ ràng: cài đặt các phụ thuộc kiểm tra bằng tay hoặc đưa chúng vào

example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
2 mặc dù chúng chỉ cần thử nghiệm. .
(Note that this does not show up as an issue in the travis, because this dependency is installed by hand here https://github.com/astropy/ccdproc/blob/master/.travis.yml#L101 )

CC: @mwcraig


Lưu ý: Ngày nay, việc sử dụng

example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3 không được khuyến khích ủng hộ
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
4 cùng với
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
5. Tìm hiểu làm thế nào để sử dụng những người ở đây.

Khi bạn đang sử dụng Python một cách chuyên nghiệp, nó trả tiền để thiết lập các dự án của bạn một cách nhất quán. Điều này giúp các cộng tác viên của bạn nhanh chóng hiểu được cấu trúc của một dự án và giúp họ dễ dàng thiết lập dự án trên máy của họ. Chìa khóa để thiết lập dự án của bạn là tệp

example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3. Trong blog này, tôi sẽ đi vào chi tiết của tập tin này.
in a consistent manner. This helps your collaborators quickly understand the
structure of a project, and makes it easier for them to set up the project
on their machine. The key to setting up your project is the
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3 file.
In this blog I’ll go into the details of this file.

Nơi chúng tôi bắt đầu

Ở đây tôi cho rằng bạn đã có một gói mà bạn muốn thiết lập. Đây không cần phải là một gói đã hoàn thành - lý tưởng nhất là bạn nên tạo

example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3 lâu trước khi dự án của bạn kết thúc. Nó thậm chí có thể là một gói trống; Chỉ cần đảm bảo thư mục gói tồn tại và chứa một tệp có tên
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
8 (có thể trống).
This does not need to be a finished package – ideally you should create the
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3 long before your project is finished. It could even be an empty package;
just make sure the package folder exists
and contains a file named
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
8 (which may be empty).

Nếu bạn theo dõi cấu trúc của đồng nghiệp Henk, cho dự án của bạn, tình huống bắt đầu của bạn sẽ trông giống như thế này:
for your project, your starting situation should look something like this:

example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.

Bạn có thể có các tệp hoặc thư mục khác trong cấu trúc của mình, ví dụ như các thư mục có tên

example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
9,
from exampleproject.example import example_function
0 hoặc
from exampleproject.example import example_function
1, nhưng những aren này được yêu cầu.
folders named
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
9,
from exampleproject.example import example_function
0 or
from exampleproject.example import example_function
1, but these aren’t required.

Trường hợp cho một
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3

Khi bạn đã tạo một gói như thế này, thì bạn có thể sử dụng một số mã ở những nơi khác. Ví dụ: bạn có thể muốn làm điều này trong một cuốn sổ tay:
to use some of the code in other places. For example, you might want
to do this in a notebook:

from exampleproject.example import example_function

Điều này sẽ hoạt động nếu thư mục làm việc hiện tại của bạn là

from exampleproject.example import example_function
3, nhưng trong tất cả các trường hợp khác, Python sẽ cung cấp cho bạn đầu ra như:
all other cases python will give you output like:

ModuleNotFoundError: No module named 'exampleproject'

Bạn có thể nói Python ở đâu để tìm gói bằng cách đặt biến môi trường

from exampleproject.example import example_function
4 hoặc thêm đường dẫn vào
from exampleproject.example import example_function
5, nhưng điều đó khác xa với lý tưởng: nó sẽ yêu cầu các hành động khác nhau trên các nền tảng khác nhau và đường dẫn bạn cần đặt tùy thuộc vào vị trí mã của bạn. Một cách tốt hơn nhiều là cài đặt gói của bạn bằng cách sử dụng
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3 và
from exampleproject.example import example_function
7, vì
from exampleproject.example import example_function
7 là cách tiêu chuẩn để cài đặt tất cả các gói khác và nó bị ràng buộc nó hoạt động giống nhau trên tất cả các nền tảng.
environment variable or adding the path to
from exampleproject.example import example_function
5,
but that is far from ideal: it would require different actions on different
platforms, and the path you need to set depends on the location of your code.
A much better way is to install your package using a
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3 and
from exampleproject.example import example_function
7,
since
from exampleproject.example import example_function
7 is the standard way to install all other packages, and it is bound
it work the same on all platforms.

Một ví dụ tối thiểu

Vậy tệp

example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3 trông như thế nào? Đây là một ví dụ tối thiểu0:

from setuptools import setup, find_packages

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*'])
)

Ở đây chúng tôi chỉ định ba điều:

  • Tên của gói, là tên mà
    from exampleproject.example import example_function
    
    7 sẽ sử dụng cho gói của bạn. Điều này không phải giống như tên thư mục mà gói hàng tồn tại, mặc dù nó có thể gây nhầm lẫn nếu không. Một ví dụ về nơi tên gói và thư mục không khớp là scikit-learn: bạn cài đặt nó bằng
    ModuleNotFoundError: No module named 'exampleproject'
    
    1, trong khi bạn sử dụng nó bằng cách nhập từ
    ModuleNotFoundError: No module named 'exampleproject'
    
    2.
    This does not have to be the same as the folder name the package lives
    in, although it may be confusing if it is not. An example of where the package
    name and the directory do not match is Scikit-Learn: you install it
    using
    ModuleNotFoundError: No module named 'exampleproject'
    
    1, while you use it by importing from
    ModuleNotFoundError: No module named 'exampleproject'
    
    2.
  • Phiên bản gói của bạn. Đây là phiên bản
    from exampleproject.example import example_function
    
    7 sẽ báo cáo và được sử dụng ví dụ khi bạn xuất bản gói của mình trên PYPI1.
    for example when you publish your package on PyPI1.
  • Những gói nào để bao gồm; Trong trường hợp của chúng tôi, đây chỉ là
    ModuleNotFoundError: No module named 'exampleproject'
    
    4. Ở đây chúng tôi để
    ModuleNotFoundError: No module named 'exampleproject'
    
    5 tự động tìm ra điều này. Mặc dù về nguyên tắc bạn có thể sử dụng
    ModuleNotFoundError: No module named 'exampleproject'
    
    6 mà không có bất kỳ đối số nào, nhưng điều này có khả năng có thể dẫn đến các gói không mong muốn được đưa vào. Điều này có thể xảy ra, ví dụ, nếu bạn đưa một
    example_project/
    ├── exampleproject/      Python package with source code.
    │   ├── __init__.py      Make the folder a package.
    │   └── example.py       Example module.
    └── README.md            README with info of the project.
    
    8 vào thư mục
    from exampleproject.example import example_function
    
    0 của bạn. Ngoài ra, bạn cũng có thể sử dụng đối số
    ModuleNotFoundError: No module named 'exampleproject'
    
    9 để ngăn chặn rõ ràng việc đưa vào các bài kiểm tra vào gói, nhưng điều này hơi kém mạnh mẽ.
    Here we let
    ModuleNotFoundError: No module named 'exampleproject'
    
    5 figure this out
    automatically2. While you could in principle use
    ModuleNotFoundError: No module named 'exampleproject'
    
    6
    without any arguments, this can potentially result in unwanted packages to
    be included. This can happen, for example,
    if you included an
    example_project/
    ├── exampleproject/      Python package with source code.
    │   ├── __init__.py      Make the folder a package.
    │   └── example.py       Example module.
    └── README.md            README with info of the project.
    
    8 in your
    from exampleproject.example import example_function
    
    0
    directory. Alternatively, you can also use the
    ModuleNotFoundError: No module named 'exampleproject'
    
    9 argument to explicitly
    prevent the inclusion of tests in the package, but this is slightly
    less robust.

Bây giờ tất cả những gì bạn cần làm để cài đặt gói của bạn là chạy các phần sau từ bên trong thư mục

from exampleproject.example import example_function
33:
from inside the
from exampleproject.example import example_function
3 directory3:

from setuptools import setup, find_packages

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*'])
)
1 ở đây đề cập đến thư mục làm việc hiện tại, mà tôi giả sử là thư mục có thể tìm thấy
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3. Cờ
from setuptools import setup, find_packages

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*'])
)
3 chỉ định rằng chúng tôi muốn cài đặt ở chế độ có thể chỉnh sửa, điều đó có nghĩa là khi chúng tôi chỉnh sửa các tệp trong gói, chúng tôi không cần cài đặt lại gói trước khi các thay đổi có hiệu lực. Bạn sẽ cần phải khởi động lại Python hoặc tải lại gói!
where the
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3 can be found. The
from setuptools import setup, find_packages

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*'])
)
3 flag specifies that we want to install
in editable mode, which means
that when we edit the files in our package we do not need to re-install the
package before the changes come into effect. You will need to either restart
python or reload the package though!

Khi bạn chỉnh sửa thông tin trong chính

example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3, bạn sẽ cần cài đặt lại gói trong hầu hết các trường hợp, và nếu bạn thêm các gói (phụ) mới. Khi nghi ngờ, nó không bao giờ có thể bị tổn thương để cài đặt lại. Chỉ cần chạy
from setuptools import setup, find_packages

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*'])
)
5 một lần nữa.
the package in most cases, and also if you add new (sub)packages.
When in doubt, it can never hurt to re-install. Just run
from setuptools import setup, find_packages

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*'])
)
5 again.

Yêu cầu

Hầu hết các dự án có một số phụ thuộc. Bạn có thể đã sử dụng tệp yêu cầu.txt trước hoặc một môi trường.yml nếu bạn đang sử dụng

from setuptools import setup, find_packages

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*'])
)
6. Bây giờ bạn đang tạo
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3, bạn có thể chỉ định các phụ thuộc của mình trong đối số
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
2. Ví dụ, đối với một dự án khoa học dữ liệu điển hình mà bạn có thể có:
a requirements.txt
file before, or an environment.yml
if you are using
from setuptools import setup, find_packages

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*'])
)
6. Now that you are creating a
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3, you can specify your
dependencies in the
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
2 argument.
For example, for a typical data science project you may have:

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)

Bạn có thể chỉ định các yêu cầu mà không có phiên bản (

from setuptools import setup, find_packages

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*'])
)
9), pin phiên bản (
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
0), chỉ định phiên bản tối thiểu (
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
1) hoặc đặt một phạm vi phiên bản (
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
2). Các yêu cầu này sẽ tự động được cài đặt bởi
from exampleproject.example import example_function
7 khi bạn cài đặt gói của mình.
version (
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
1) or set a range of versions (
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
2). These
requirements will automatically be installed by
from exampleproject.example import example_function
7 when you install your package.

Đôi khi bạn có thể có những phụ thuộc chỉ được yêu cầu trong một số tình huống nhất định. Là một nhà khoa học dữ liệu, tôi thường thực hiện các gói mà tôi sử dụng để đào tạo một mô hình. Khi tôi làm việc trên một mô hình như vậy một cách tương tác, tôi có thể cần phải cài đặt

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
4 và
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
5 để hoạt động tương tác với dữ liệu và tạo trực quan hóa hiệu suất của mô hình. Mặt khác, nếu mô hình chạy trong sản xuất, tôi không muốn cài đặt
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
4 cũng như
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
5 trên máy (hoặc container) nơi tôi đào tạo hoặc suy luận. May mắn thay
ModuleNotFoundError: No module named 'exampleproject'
5 cho phép chỉ định các phụ thuộc tùy chọn trong
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
9:
I often make packages which I use to train a model. When I work on such a model interactively
I may need to have
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
4 and
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
5 installed in order to interactively work with the
data and to create visualizations
of the performance of the model. On the other hand, if the model runs in production I do not
want to install
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
4 nor
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
5 on the machine (or container) where I train
or do inference. Luckily
ModuleNotFoundError: No module named 'exampleproject'
5 allows to specify optional dependencies in
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
9:

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5'
    ],
    extras_require={
        'interactive': ['matplotlib>=2.2.0', 'jupyter'],
    }
)

Bây giờ nếu chúng tôi cài đặt gói bình thường (

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5'
    ],
    extras_require={
        'interactive': ['matplotlib>=2.2.0', 'jupyter'],
    }
)
0 từ PYPI hoặc
from setuptools import setup, find_packages

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*'])
)
5 cục bộ), nó sẽ chỉ cài đặt các phụ thuộc
from setuptools import setup, find_packages

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*'])
)
9,
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5'
    ],
    extras_require={
        'interactive': ['matplotlib>=2.2.0', 'jupyter'],
    }
)
3 và
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5'
    ],
    extras_require={
        'interactive': ['matplotlib>=2.2.0', 'jupyter'],
    }
)
4. Tuy nhiên, khi chúng tôi chỉ định rằng chúng tôi muốn các phụ thuộc
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5'
    ],
    extras_require={
        'interactive': ['matplotlib>=2.2.0', 'jupyter'],
    }
)
5 tùy chọn (
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5'
    ],
    extras_require={
        'interactive': ['matplotlib>=2.2.0', 'jupyter'],
    }
)
6 hoặc
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5'
    ],
    extras_require={
        'interactive': ['matplotlib>=2.2.0', 'jupyter'],
    }
)
7), thì
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
4 và
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
5 cũng sẽ được cài đặt.
it will only install the dependencies
from setuptools import setup, find_packages

setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*'])
)
9,
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5'
    ],
    extras_require={
        'interactive': ['matplotlib>=2.2.0', 'jupyter'],
    }
)
3 and
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5'
    ],
    extras_require={
        'interactive': ['matplotlib>=2.2.0', 'jupyter'],
    }
)
4. However, when we specify
that we want the optional
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5'
    ],
    extras_require={
        'interactive': ['matplotlib>=2.2.0', 'jupyter'],
    }
)
5 dependencies (
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5'
    ],
    extras_require={
        'interactive': ['matplotlib>=2.2.0', 'jupyter'],
    }
)
6
or
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5'
    ],
    extras_require={
        'interactive': ['matplotlib>=2.2.0', 'jupyter'],
    }
)
7),
then
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
4 and
setup(
    name='example',
    version='0.1.0',
    packages=find_packages(include=['exampleproject', 'exampleproject.*']),
    install_requires=[
        'PyYAML',
        'pandas==0.23.3',
        'numpy>=1.14.5',
        'matplotlib>=2.2.0,,
        'jupyter'
    ]
)
5 will also be installed.

Kịch bản và điểm nhập cảnh

Trường hợp sử dụng chính của hầu hết các gói Python mà bạn cài đặt từ PYPI là cung cấp chức năng có thể được sử dụng trong mã Python khác. Nói cách khác, bạn có thể

setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
0 từ các gói đó. Là một nhà khoa học dữ liệu, tôi thường tạo ra các gói mà aren có nghĩa là được sử dụng bởi mã Python khác nhưng có nghĩa là làm một cái gì đó, ví dụ như để đào tạo một mô hình. Như vậy, tôi thường có một tập lệnh Python mà tôi muốn thực thi từ dòng lệnh.
that can be used in other python code. In other words, you can
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
0 from those packages.
As a data scientist I often make packages that aren’t meant to be used by other python code but
are meant to do something, for example to train a model. As such, I often have a python script that
I want to execute from the command line.

Cách tốt nhất để phơi bày chức năng của gói của bạn với dòng lệnh là xác định

setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
1 như vậy:
an
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
1 as such:

setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)

Bây giờ bạn có thể sử dụng lệnh

setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
2 từ dòng lệnh, từ đó sẽ thực thi hàm
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
3 bên trong
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
4. Đừng quên cài đặt lại-nếu không lệnh sẽ không được đăng ký.
function inside
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
4. Do not forget to re-install – otherwise the command
will not be registered.

Xét nghiệm

Bất cứ khi nào bạn viết bất kỳ mã nào, tôi thực sự khuyến khích bạn cũng viết các bài kiểm tra cho mã này. Để thử nghiệm với Python, tôi khuyên bạn nên sử dụng

setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
5. Tất nhiên bạn không muốn thêm
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
5 vào các phụ thuộc của mình trong
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
2: Đó là yêu cầu của người dùng gói của bạn. Để cài đặt nó tự động khi bạn chạy thử nghiệm, bạn có thể thêm phần sau vào
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3 của mình:
with python I suggest you use
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
5. Of course you do not want to add
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
5 to your dependencies
in
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
2: it isn’t required by the users of your package. In order to have it installed
automatically when you run tests you can add the following to your
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3:

[testenv]
...
extras = tests
...
0

Ngoài ra, bạn sẽ phải tạo một tệp có tên

example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
5 với các nội dung sau:

Bây giờ bạn có thể chỉ cần chạy

        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
6 và
ModuleNotFoundError: No module named 'exampleproject'
5 sẽ đảm bảo các phụ thuộc cần thiết được cài đặt và chạy
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
5 cho bạn! Hãy xem ở đây nếu bạn muốn cung cấp các đối số hoặc đặt các tùy chọn cấu hình cho
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
5.
are installed and run
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
5 for you! Have a look here if
you want to provide arguments or set configuration options for
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
5.

Nếu bạn có bất kỳ yêu cầu bổ sung nào để kiểm tra (ví dụ:

[testenv]
...
extras = tests
...
04), bạn có thể thêm chúng vào
        fixture 'caplog' not found                                               
        available fixtures: tmpdir_factory, pytestconfig, ccd_data, cache, recwarn, monkeypatch, triage_setup, record_xml_property, capfd, capsys, tmpdir         
        use 'py.test --fixtures [testpath]' for help on them.                    
5.

Flake8

Cá nhân tôi nghĩ rằng đó là một ý tưởng tốt để chạy Flake8 để kiểm tra định dạng mã của bạn. Giống như với

setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
5, bạn không muốn thêm
[testenv]
...
extras = tests
...
07 vào các phụ thuộc
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
2: Không cần phải cài đặt để sử dụng gói của bạn. Thay vào đó, bạn có thể thêm nó vào
[testenv]
...
extras = tests
...
09:
check the formatting of your code. Just like with
setup(
    # ...,
    entry_points={
        'console_scripts': ['my-command=exampleproject.example:main']
    }
)
5, you do not want to add
[testenv]
...
extras = tests
...
07 to the
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
2 dependencies: it does not need to be installed in order to use your
package. Instead, you can add it to
[testenv]
...
extras = tests
...
09:

[testenv]
...
extras = tests
...
1

Bây giờ bạn chỉ có thể chạy

[testenv]
...
extras = tests
...
10. Tất nhiên bạn cũng có thể ghim phiên bản
[testenv]
...
extras = tests
...
07 (hoặc bất kỳ gói nào khác) trong
[testenv]
...
extras = tests
...
09.
of
[testenv]
...
extras = tests
...
07 (or any other package) in
[testenv]
...
extras = tests
...
09.

Nếu bạn muốn thay đổi một số tham số cấu hình của Flake8, bạn có thể thêm phần

[testenv]
...
extras = tests
...
13 vào
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
5 của bạn. Ví dụ:
your
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
5. For example:

[testenv]
...
extras = tests
...
2

Dữ liệu gói

Đôi khi bạn có thể muốn bao gồm một số tệp không python trong gói của bạn. Ví dụ, chúng có thể là các tệp lược đồ hoặc bảng tra cứu nhỏ. Xin lưu ý rằng các tệp như vậy sẽ được đóng gói cùng với mã của bạn, vì vậy nói chung là một ý tưởng tồi để bao gồm bất kỳ tệp lớn nào.
may for example be schema files or a small lookup table. Be aware that such files
will be packaged together with your code, so it is in general a bad idea to include
any large files.

Giả sử chúng ta có một

[testenv]
...
extras = tests
...
15 trong dự án của chúng ta, mà chúng ta đặt vào
[testenv]
...
extras = tests
...
16. Nếu chúng tôi muốn đưa cái này vào gói của mình, chúng tôi phải sử dụng đối số
[testenv]
...
extras = tests
...
17 của
[testenv]
...
extras = tests
...
18:
If we want to include this in our package, we must use the
[testenv]
...
extras = tests
...
17 argument of
[testenv]
...
extras = tests
...
18:

[testenv]
...
extras = tests
...
3

Điều này sẽ đảm bảo tập tin được bao gồm trong gói. Chúng tôi cũng có thể chọn bao gồm tất cả các tệp dựa trên một mẫu, ví dụ:
all files based on a pattern, for example:

[testenv]
...
extras = tests
...
4

Điều này sẽ thêm tất cả các tệp

[testenv]
...
extras = tests
...
19 trong bất kỳ gói nào mà nó gặp phải.

Bây giờ don lồng cố gắng tự mình tìm ra vị trí của các tệp đã cài đặt, vì

[testenv]
...
extras = tests
...
20 có một số chức năng tiện lợi rất tiện dụng:
[testenv]
...
extras = tests
...
20 has some very handy convenience functions:

  • [testenv]
    ...
    extras = tests
    ...
    
    21 sẽ cung cấp cho bạn một luồng tệp, giống như đối tượng bạn nhận được khi bạn gọi
    [testenv]
    ...
    extras = tests
    ...
    
    22,
    object you get when you call
    [testenv]
    ...
    extras = tests
    ...
    
    22,
  • [testenv]
    ...
    extras = tests
    ...
    
    23 sẽ cung cấp cho bạn nội dung của tệp dưới dạng chuỗi,
  • [testenv]
    ...
    extras = tests
    ...
    
    24 sẽ cung cấp cho bạn tên tệp của tệp (và trích xuất nó vào tạm thời nếu nó được bao gồm trong gói nén) vì nếu hai tùy chọn ở trên không phù hợp với nhu cầu của bạn.
    it into a temporary if it is included in a zipped package) for if the two options
    above do not suit your needs.

Ví dụ: chúng ta có thể đọc trong lược đồ của mình bằng cách sử dụng:

[testenv]
...
extras = tests
...
5

metadata

Nếu bạn định xuất bản gói của mình, thì có lẽ bạn muốn cung cấp cho người dùng tiềm năng của mình thêm một số thông tin về gói của bạn, bao gồm mô tả, tên của tác giả hoặc người bảo trì và URL vào trang chủ gói. Bạn có thể tìm thấy một danh sách đầy đủ của tất cả các siêu dữ liệu được phép trong các tài liệu

ModuleNotFoundError: No module named 'exampleproject'
5.
potential users some more information about your package, including a description,
the name of the author or maintainer, and the url to the package’s home page.
You can find a complete list of all allowed metadata in the
ModuleNotFoundError: No module named 'exampleproject'
5
docs.

Ngoài ra, nếu bạn định xuất bản lên PYPI, thì bạn có thể muốn tự động tải nội dung của

[testenv]
...
extras = tests
...
26 vào
[testenv]
...
extras = tests
...
27 và cung cấp các phân loại để nói với
from exampleproject.example import example_function
7 thậm chí nhiều hơn về gói của bạn.
automatically load the contents of your
[testenv]
...
extras = tests
...
26
into the
[testenv]
...
extras = tests
...
27,
and provide classifiers to tell
from exampleproject.example import example_function
7 even
more about your package.

Wrap-up

Blog này nên là một điểm khởi đầu tốt để thiết lập hầu hết các dự án Python của bạn. Nếu bạn muốn đọc thêm về bao bì Python, hãy xem các tài liệu. Dưới đây là một ví dụ

example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3 kết hợp tất cả các phần được hiển thị trong blog này:
If you want to read more about python packaging have a look
at the docs. Here is an example
example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
3
which combines all parts shown in this blog:

[testenv]
...
extras = tests
...
6

example_project/
├── exampleproject/      Python package with source code.
│   ├── __init__.py      Make the folder a package.
│   └── example.py       Example module.
└── README.md            README with info of the project.
5 đi kèm:

[testenv]
...
extras = tests
...
7

Cải thiện kỹ năng Python của bạn, học hỏi từ các chuyên gia!

Tại Godatadriven, chúng tôi cung cấp một loạt các khóa học Python từ người mới bắt đầu đến chuyên gia, được giảng dạy bởi các chuyên gia giỏi nhất trong lĩnh vực này. Tham gia với chúng tôi và lên cấp trò chơi Python của bạn:

  • Python Essentials - Tuyệt vời nếu bạn mới bắt đầu với Python.
  • Khoa học dữ liệu được chứng nhận với Python Foundation - Bạn muốn bước lên từ phân tích dữ liệu và trực quan hóa đến khoa học dữ liệu thực? Đây là khóa học phù hợp.
  • Khoa học dữ liệu nâng cao với Python - Học cách sản xuất các mô hình của bạn như một pro và sử dụng python để học máy.
Chú thích

0: Trong blog này, tôi đã sử dụng setuptools để thiết lập dự án ví dụ của mình. Ngoài ra, bạn cũng có thể sử dụng Distutils, đây là công cụ tiêu chuẩn để đóng gói trong Python, nhưng nó thiếu các tính năng như hàm

ModuleNotFoundError: No module named 'exampleproject'
6 và
[testenv]
...
extras = tests
...
32. Vì việc sử dụng setuptools ngày nay rất phổ biến và nhiều tính năng của nó có thể đặc biệt hữu ích, tôi khuyên bạn nên sử dụng setuptools.
: In this blog I have used setuptools
to set up my example project. Alternatively
you could also use distutils,
which is the standard tool for packaging in python, but it lacks features
such as the
ModuleNotFoundError: No module named 'exampleproject'
6 function and
[testenv]
...
extras = tests
...
32.
Since the use of setuptools is very common nowadays and many of its features
can be particularly useful, I suggest that you should use setuptools.

1: Nếu bạn muốn phiên bản gói của bạn cũng có sẵn bên trong Python, hãy xem ở đây.: If you want the version of your package to also be available inside python,
have a look here.

2: Bạn cũng có thể liệt kê các gói của mình theo cách thủ công, nhưng điều này đặc biệt dễ bị lỗi.: You could also list your packages manually, but this is particularly error-prone.

3: Ngoài ra, bạn có thể chạy

[testenv]
...
extras = tests
...
33, nhưng sử dụng
from exampleproject.example import example_function
7 có nhiều lợi ích, trong đó là tự động cài đặt các phụ thuộc và khả năng gỡ cài đặt hoặc cập nhật gói của bạn.
: Alternatively you could run
[testenv]
...
extras = tests
...
33, but using
from exampleproject.example import example_function
7 has
many benefits, among which are automatic installation of dependencies and the
ability to uninstall or update your package.

4: Bạn cũng có thể sử dụng đối số

[testenv]
...
extras = tests
...
35 (xem ví dụ ở đây) nhưng vì điều này đòi hỏi bạn phải tạo tập lệnh Python Shell, nó có thể không hoạt động tốt (hoặc hoàn toàn) trên Windows.: You could also use the
[testenv]
...
extras = tests
...
35 argument (see for
example here)
but as this requires you to create a python shell script it may not work
as well (or at all) on Windows.

Theo dõi bản tin của chúng tôi

Luôn cập nhật những hiểu biết mới nhất và thực hành tốt nhất bằng cách đăng ký nhận bản tin Godatadriven.

Làm cách nào để cài đặt các phụ thuộc Python?

Cài đặt phụ thuộc Python..
Cài đặt gói từ PYPI ..
Cài đặt từ kho lưu trữ công khai ..
Cài đặt từ kho lưu trữ đăng ký cổ vật ..
Cài đặt từ kho lưu trữ riêng ..
Cài đặt thư viện Python cục bộ ..
Sử dụng các gói phụ thuộc vào các thư viện đối tượng được chia sẻ ..

Cài đặt có bị lỗi thời không?

... Tính đến vài năm qua, tất cả các yêu cầu trực tiếp của setup.py được phản đối một cách hiệu quả để ủng hộ các yêu cầu thông qua các công cụ CLI được xây dựng có mục đích và/hoặc các tiêu chuẩn như PIP, Build và Tox.all direct invocations of setup.py are effectively deprecated in favor of invocations via purpose-built and/or standards-based CLI tools like pip, build and tox.

Kiểm tra cài đặt PIP là gì?

Mục đích của gói này là cung cấp một mô -đun đơn giản có thể dễ dàng cài đặt và gỡ cài đặt.Điều này cung cấp một cơ chế đơn giản để chỉ ra rằng một môi trường tùy ý có khả năng cài đặt các mô -đun bên thứ ba thông qua PIP.provide a simple module that can be easily installed and uninstalled. This provides a straightforward mechanism for showing that an arbitrary environment is capable of installing third party modules via pip.

Pip có tự động cài đặt không?

PIP dựa vào các tác giả gói để quy định các phụ thuộc cho mã của họ để tải xuống thành công và cài đặt gói cộng với tất cả các phụ thuộc cần thiết từ Chỉ số gói Python (PYPI).Nhưng nếu các gói được cài đặt cùng một lúc, nó có thể dẫn đến xung đột phụ thuộc.