This is the 19th day of my participation in the August Wenwen Challenge.More challenges in August

Review and

We have studied modules and packages in detail in previous articles. For example:

  • In modular programming, I learned the concepts and programming ideas related to modules
  • Module import Learning module import import and from… Import methods and details
  • In package analysis, master how to create and import packages.

All the above are in the module import and other links in our module development. When our module is developed, we should package and release the developed module, provide it to other projects for use, or upload it to the public server, so that others can download and use the developed module.

There are two main types of module publishing: local publishing and uploading to third-party networks

📌 We suddenly wondered, how to package the project engineering code for release?

Today we are going to learn about module release and installation, Let’s go~

1. The module is published locally

When we have finished developing our modules, we can release them to the public, and other developers can use our modules as “third party extensions”.

Our module local release mainly has the following steps:

  1. Create a module folder

    (1) Put the module code to be released into the file

    (2) The folder name and module name, generally keep the same

    (3) For example, the directory structure of the module module_hello file to be published is as follows

    The content of the module_hello file is

    Def hello(name): print("hello {0},welcome to juejin". Format (name))Copy the code
  2. Create a file named [setup.py] in the folder

    (1) Setup. py file stores module metadata, description related information

    The first line of the setup.py file must import the Setup function of the Python publishing tool

      ```
        from distutils.core import setup
      ```
    Copy the code

    For example, create setup.py in the hello folder. The contents of setup.py are as follows

    From distutils.core import setup setup(name = 'hellojuejin', version = '1.1.0', py_modules = ['hellojuejin.module_hello'], author = 'Anne_cute', author_email = '****@qq.com', url = 'http://www.hp.com', description = 'hello Juejin', ) ```Copy the code
  3. In the terminal window, enter the command python setup.py sdist,

2. Install the module locally

Install the release on your local computer, still in terminal CMD command line mode

The procedure for installing a local module is as follows:

  1. Go to the setup.py directory, open the CMD window, and enter sudo python setup.py install

  2. After the installation is successful, you can go to the Python third-party module directory to view it

    (1) The third-party module library is installed in the Python directory /lib/site-packages

    (2) You can also see the module helloJueJin installed in Pycharm

  3. After the installation is successful, use Import directly in Pycharm to import

        import hellojuejin.module_hello
        hellojuejin.module_hello.hello("Anne")
    Copy the code

3. Upload the module to PyPI

Upload your developed modules to the PyPI website, which will become a public resource for users around the world to use freely

The main steps of module publishing on PyPI website are as follows:

  1. Register on the PyPI website

    (1) PyPI website address: pypi.python.org

    (2) After the registration information is filled in and submitted, the mailbox will receive the verification information for confirmation

  2. Create user information file. Pyppirc

    • Method 1: Only for Linux: Run a command

      (1) Enter and execute Python setup.py register (2) Enter a user and password

    • Method 2: For Windows and Linux: Use a file

      (1) Create a file named. Pypirc in the user directory

      [distutils] index - the servers = pypi [pypi] repository username = = https://upload.pypi.org/legacy/ registered user name password = just now The password you just registeredCopy the code

    📢 note:

    • . Pypirc File path:

      (1) Linux user directory: ~/.pypirc

      (2) Windows user directory: C :/user/ username /.pypirc

      ⭐ Notice In Windows, the creation name must be.pypirc

  3. In the setup.py file directory, enter the command python setup.py sdist upload from the console to upload the publishing module

  4. Open the PyPI website to view the published modules

4. Others download and install the module

We can download third-party libraries on our computers through PIP. There are two main ways:

  • Method – : On the terminal, enter the PIP install module name

  • Method 2: Install the pyCharm library and search for downloads

📢 because the module has just been uploaded, the official website is not updated so quickly, so the search will lag behind

conclusion

In this issue, we learn the mode of module release mainly includes local release and third-party website PyPI release.

Developed modules can be used locally or on the server, so that more people can experience the joy of Python programming

The above is the content of this issue, welcome to the big people like the comments and corrections, see next time ~ღ(´ ᴗ · ‘) than the heart 🌹🌹 ᴗ Stan ღ