Mục đích của hướng dẫn này là ghi lại các thực tiễn, mẹo và thủ thuật tốt nhất để phát triển Python. Đối với tập lệnh và gói ... TBD Mục đích của phần này là cung cấp một hướng dẫn để làm theo khi bạn muốn định cấu hình dự án Python và được triển khai thành PYPI. Tổ chức dự án Đăng nhập Kiểm tra Đăng nhậpMẫu
Thư viện xuất bản thành Pypi
Pypi
Tổ chức dự án
name-of-your-project
├── docs
│ ├── imgs
│ └── doc.md
├── name_of_your_project
│ ├── __init__.py
│ └── code.py
├── tests
│ └── test_code.py
├── .gitignore
├── .travis.yml
├── HISTORY.md
├── LICENSE
├── Makefile
├── README.md
├── requirements_dev.txt
├── setup.cfg
├── setup.py
├── tox.ini
Setup.py
Kiểm tra
Here you have an example of how an Ocean setup.py file looks like:
#!/usr/bin/env python # -*- coding: utf-8 -*- """The setup script.""" from setuptools import setup, find_packages with open['README.md'] as readme_file: readme = readme_file.read[] with open['HISTORY.md'] as history_file: history = history_file.read[] requirements = [YOUR_REQUIREMENTS] setup_requirements = ['pytest-runner', ] test_requirements = ['pytest', ] setup[ author="leucothia", author_email='', classifiers=[ 'Development Status :: 2 - Pre-Alpha', 'Intended Audience :: Developers', 'License :: OSI Approved :: Apache Software License', 'Natural Language :: English', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', ], description=" 🐳 DESCRIPTION_OF_YOUR_PACKAGE", install_requires=requirements, license="Apache Software License 2.0", long_description=readme + + '\n\n' + history, long_description_content_type="text/markdown", include_package_data=True, keywords='REPO_NAME', name='REPO_NAME', packages=find_packages[include=['YOUR_PACKAGES']], setup_requires=setup_requirements, test_suite='tests', tests_require=test_requirements, url='//github.com/oceanprotocol/REPO_NAME', version='0.0.1', zip_safe=False, ]
Tox
Hội nhập liên tục
Travis Ci
Bumpversion
Thử nghiệm trong Pycharm
Quản lý gói địa phương và môi trường ảo
Đăng nhập
Kiểm tra
from package_dir.log import setup_logging
import logging
setup_logging[]
Tox
Hội nhập liên tục
Travis Ci
Bumpversion
version: 1
disable_existing_loggers: False
formatters:
simple:
format: "%[name]-5s - %[levelno]-3s - %[module]-20s %[funcName]-30s: %[message]s"
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
loggers:
my_module:
level: ERROR
handlers: [console]
propagate: no
root:
level: DEBUG
handlers: [console]
Thử nghiệm trong Pycharm
Quản lý gói địa phương và môi trường ảo
Sử dụng PIP và VirtualEnV
version: 1 disable_existing_loggers: False formatters: simple: format: "%[asctime]s - %[name]s - %[levelname]s - %[message]s" handlers: console: class: logging.StreamHandler level: DEBUG formatter: simple stream: ext://sys.stdout info_file_handler: class: logging.handlers.RotatingFileHandler level: INFO formatter: simple filename: info.log maxBytes: 10485760 # 10MB backupCount: 20 encoding: utf8 error_file_handler: class: logging.handlers.RotatingFileHandler level: ERROR formatter: simple filename: errors.log maxBytes: 10485760 # 10MB backupCount: 20 encoding: utf8 loggers: my_module: level: ERROR handlers: [console] # Ignore the file handlers until deployment propagate: no root: level: INFO handlers: [console, info_file_handler, error_file_handler]
Kiểm tra
Tox
Hội nhập liên tục
Hội nhập liên tục
Travis Ci
Bumpversion
language: python
python:
- 3.6
- 3.5
- 3.4
# Command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
install: pip install -U tox-travis
script: tox
deploy:
provider: pypi
distributions: sdist bdist_wheel
user: leucothia
password:
secure: LEUCOTHIA_PASSWORD_ENCRYPTED
on:
tags: true
branch: master
repo: oceanprotocol/REPO_NAME
python: 3.6
Thử nghiệm trong Pycharm
echo -n LEUCOTHIA_PASSWORD | travis encrypt --add deploy.password -r oceanprotocol/REPO_NAME
Bumpversion
Thử nghiệm trong Pycharm
Thử nghiệm trong Pycharm
Quản lý gói địa phương và môi trường ảo
Sử dụng PIP và VirtualEnV
Sử dụng pipenv
Quản lý gói địa phương và môi trường ảo
Sử dụng PIP và VirtualEnV
Sử dụng pipenv
virtualenv -p $[which python3.6] py36 # Create a Python 3.6 virtualenv named py36 source py36/bin/activate # Make the py36 virtualenv active python --version # Check the version of Python in the virtualenv pip install blahblah # Install the Python package named blahblah from PyPI pip freeze # List the Python packages installed in the virtualenv deactivate # Deactivate the current virtualenv [py36] source other_env/bin/activate # activate other_env
Sử dụng Conda
- IDE và biên tập viên
- Phòng thí nghiệm Jupyter
- Nguyên tử
- Spyder
Eclipse + Pydev
Sử dụng pipenv
Trình quản lý gói được đề xuất PYPA hiện tại.Pipenv trên github.
Sử dụng Conda
Hiện tại, các gói không được công bố cho kho lưu trữ gói Anaconda.Tuy nhiên, bạn có thể sử dụng Conda làm Trình quản lý môi trường ảo và
from package_dir.log import setup_logging
import logging
setup_logging[]
5 gói Python vào môi trường Conda của bạn.Dọn dẹp thư mục dự án
Sau khi làm việc với một dự án Python trong một thời gian, thư mục dự án có thể nhận được đầy đủ các tạo tác và đôi khi những thứ đó có thể gây ra vấn đề [ví dụ: xung đột].Bạn có thể làm sạch chúng bằng cách sử dụng:
# Remove build artifacts rm -fr build/ rm -fr dist/ rm -fr .eggs/ find . -name '*.egg-info' -exec rm -fr {} + find . -name '*.egg' -exec rm -f {} + # Remove Python file artifacts find . -name '*.pyc' -exec rm -f {} + find . -name '*.pyo' -exec rm -f {} + find . -name '*~' -exec rm -f {} + find . -name '__pycache__' -exec rm -fr {} + # Remove test and coverage artifacts find . -name '.pytest_cache' -exec rm -fr {} + rm -fr .tox/ rm -f .coverage rm -fr htmlcov/
Mẹo: Nếu bạn gặp lỗi về quyền, thì hãy tự cho mình các quyền cần thiết bằng cách sử dụng
from package_dir.log import setup_logging
import logging
setup_logging[]
6IDE và biên tập viên
Phòng thí nghiệm Jupyter
from package_dir.log import setup_logging
import logging
setup_logging[]
7 Trong mỗi môi trường để chuyển đổi hạt nhân hoạt động!Nguyên tử
Spyder
Lắp đặt Spyder trong mỗi VENV để đảm bảo sử dụng kernel.