Python builds a project

Source: Internet
Author: User
Tags virtualenv

Second, experiment Step 2.1 experiment preparation

Our experimental project is named factorial.

cd factorial/
2.2 Main Code

We named the Python module we are going to create as myfact, so we'll create the myfact directory next.

cd myfact/

The main code will be inside the fact.py file.

"myfact module"def factorial(num): """ 返回给定数字的阶乘值 :arg num: 我们将计算其阶乘的整数值 :return: 阶乘值,若传递的参数为负数,则为 -1 """ if num >= 0: if num == 0: return 1 return num * factorial(num -1) else: return -1

We also have the module __init__.py file, which reads as follows:

from fact import factorial__all__ = [factorial, ]

We have also added a readme.rst file under the factorial directory. Therefore, the directory structure looks like this:

2.3 manifest.in

Now we're going to write a MANIFEST.in file that is used to find all the files that will be part of the project's source code compression package when using the sdist command.

include *.pyinclude README.rst

If you want to exclude certain files, you can use the exclude statement in this file.

2.4 Installing the Python-setuptools Package

We use virtualenv(no demonstration steps here).

$ sudo pip3 install setuptools
2.5 setup.py

Eventually we need to write a setup.py file to create the source code to compress the package or install the software.

 #!/usr/bin/env python3 "" "Factorial Project" "from setuptools import find_packages, Setupsetup (name =  ' factorial ', Version =  ' 0.1 ', description = Span class= "hljs-string" > "factorial module.", long_description =  "A test module for our book." , platforms = [ "Linux"], Author= "Shiyanlou", Author_email= "[email protected]", Url= "https://www.shiyanlou.com/courses/ 596 ", license = " MIT ", Packages=find_packages ())      

name is the project title , version is the release,description and long_description are the project descriptions, and the project length is described. platforms is a list of supported platforms for this module. find_packages () is a special function that can find all the modules in your source directory, packaging Docs.

2.5.1. setup.py use case

To create a release version of the source file, execute the following command.

$ python3 setup.py sdist

After execution, a message similar to the following is returned:

Running sdistrunning egg_infocreating factorial.egg-infowriting factorial.egg-info/pkg-infowriting top-level names to Factorial.egg-info/top_level.txtwriting Dependency_links to Factorial.egg-info/dependency_links.txtwriting Manifest file' Factorial.egg-info/sources.txt ' reading manifest file' Factorial.egg-info/sources.txt ' reading manifest template' manifest.in ' writing MANIFEST file ' factorial.egg-info/sources.txt ' running checkcreating factorial-0.1creating Factorial-0.1/factorial.egg-infocreating factorial-0.1/myfactmaking Hard links in Factorial-0.1...hard linking manifest.in, Factorial-0.1hard linking Readme.rst, Factorial-0.1hard linking setup.py, Factorial-0.1hard linking Factorial.egg-info/pkg-info, Factorial-0.1/factorial.egg-infohard Linking Factorial.egg-info/sources.txt, Factorial-0.1/factorial.egg-infohard linking factorial.egg-info/ Dependency_links.txt, Factorial-0.1/factorial.egg-infohard linking Factorial.egg-info/top_level.txt, Factorial-0.1/factorial.egg-infohard linking myfact/__init__.py, Factorial-0.1/myfacthard linking myfact/ fact.py-factorial-0.1/myfactwriting factorial-0.1/setup.cfgcreating distcreating tar archiveremoving  ' factorial-0.1 ' (and everything under it)   

We can see a tar compress package in the Dist directory.

$ ls dist/factorial-0.1.tar.gz

Remember to use virtualenv when trying to install code.

Execute the following command from the source code installation.

$ sudo python3 setup.py install

Learn more to go to packaging.python.org.

2.6. Python Package Index (PyPI)

Do you remember the pip Command We used a lot? Have you ever wondered where these bags came from? The answer is PyPI. This is the Python package management system.

For experimentation, we will use PyPI 's test server https://testpypi.python.org/pypi.

2.6.1. Create an Account

Register your account with this link first. You will receive an email with a link, click on this link to confirm your registration.

Create a ~/.PYPIRC file that holds your account details in the following format:

[distutils]index-servers = pypi    <username>password: <password>[testpypi]repository:https://test.pypi.org/legacy/username: <username>password: <password>

Replace <username> and <password> details for your newly created account. Here, because we are to testpypi the page to register the account, the corresponding service will be uploaded testpypi , so here, you only need to modify [testpypi] the user name and password

Remember to change the name of the project in the setup.py other name to test the following instruction, in the following command I changed the project name to Factorial2, in order not to repeat, I need to modify to other names.

2.6.2. Uploading to the Testpypi service

Next we will take our project to the Testpypi service. This is twine done by command.

We will also use -r it to point to the test server.

$ sudo pip3 install twine$ twine upload dist/* -r testpypi

After execution, a message similar to the following will be returned.

Uploading distributions to https://test.pypi.org/legacy/Uploading factorial2-0.1.tar.gz

Here you can also use the following command to upload to the PyPI service, but it is important to note that in the ~/.pypirc inside, you need to go to the https://pypi.python.org page, follow the above steps to register an account, and then to ~/.pypirc the [pypi] next fill in the corresponding user name and password. testpypiand pypi The account password is not universal.

$ twine upload dist/* -r pypi

Now if you browse this page, you will find that your project is ready to be used by others.

Iii. Summary

This experiment used the Setuptools package, completed a more complete project creation & release process, and finally published the Project on the Network (PyPI).

Python builds a project

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.