How to use PIP to install your own third-party module in Python

Source: Internet
Author: User

System: CentOS6.8 python version: python3.5

Text Editor: Vim

Most of the time in Python we need to import our own py file into the Python path, but our own files are not in the Python default library file path, and we need to import our py file into the

The following example how to import their own third-party module, we first need to create a. py file, the name is randomly named, but not with the Python internal module, internal module path in/usr/lib/python3.5/site-packages. Otherwise, it would be tragic to use some of the modules provided by Python.

We write the code to see if a list is nested with a list, and if it is nested, the output is disassembled, the environment Python3, named liname.py

#!/usr/bin/env Python3
def print_list (the_list): Outgoing a list parameter for item in The_list: Iterate through the list element if Isinstance (i Tem,list): Determine whether each element is a list print_list (item) call itself to do Else:print (item)

We want to turn this. py file into a module, and we can use the Python package index (Python packages Index,pypi) for the centralized management of the third library in the Internet, we first create a directory, the name is casual, for example, called Dog

In this directory, create a file with the name setup.py, and the contents of the file are as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/89/E6/wKiom1ggw67ThVQNAAA2f4wO3PU932.png "title=" Capture 5. PNG "alt=" Wkiom1ggw67thvqnaaa2f4wo3pu932.png "/>

Now dog This directory contains two files setup.py and lsname.py This file, we enter the command in the terminal: Python3 setup.py sdist, and then the directory will be more than a few files and directories,

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/89/E3/wKioL1ggxJrhQ333AABZs6sWCz8636.png "title=" Capture 7. PNG "alt=" Wkiol1ggxjrhq333aabzs6swcz8636.png "/> Then we use the command: Python3 setup.py install installation

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/89/E6/wKiom1ggxOrA5WWtAAAz4r9tK4s494.png "title=" Capture 8. PNG "alt=" Wkiom1ggxora5wwtaaaz4r9tk4s494.png "/>

Then we python3 the interactive command line to import the module, enter a list, and then call, will trigger the following exception

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/89/E3/wKioL1ggxiuwawmnAAAriK1ST08121.png "title=" Capture 9. PNG "alt=" Wkiol1ggxiuwawmnaaarik1st08121.png "/>

This is because the code modules in our Python main program are associated with a namespace named __main__, and Python automatically creates a namespace with the same name as the module when the code is placed in its own module. So the code in our module is associated with a namespace named Liname.

When we first entered the interactive command line of Python, it was associated with the __main__ namespace, and we wanted to use the Print_list method of Liname as a way to use this function without using liname.print_list ( This is used like this:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/89/E3/wKioL1ggzS3AkLS1AAAV1rZayNs417.png "title=" capture 10. PNG "alt=" Wkiol1ggzs3akls1aaav1rzayns417.png "/>

There is no problem at all.

If we write a good piece of code ourselves and want to contribute it, we can put it on the PyPI site, except for GitHub, where we install with a command like PIP or Easy_install.

We will first register our information on the PyPI website (http://pypi.python.org/).

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/89/E6/wKiom1ggzlmA3850AAAsfvM1YUM696.png "title=" Capture 11. PNG "alt=" Wkiom1ggzlma3850aaasfvm1yum696.png "/>

After the registration is done we start to upload our code to PyPI, we need to know when we register the account and password, because the next will use 650) this.width=650; "Src=" http://s1.51cto.com/wyfs02/M02/89/ E3/wkiol1ggzwjaxxw1aaa1m2vv-sm013.png "title=" capture 12. PNG "alt=" Wkiol1ggzwjaxxw1aaa1m2vv-sm013.png "/>

Because I have no good code contribution so, the next step is omitted, after the steps are completed, we can upload,

We use the command: Python3 setup.py upload

At this point our own code has been contributed to the PyPI community, if others want to install our module, you can use PIP or Easy_install this way to install our module

This article is from the "berniem2m" blog, make sure to keep this source http://berniem2m.blog.51cto.com/9203249/1870497

How to use PIP to install your own third-party module in Python

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.