Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between modlue and packages in Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the difference between modlue and packages in Python? many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

First open Pycharm (unused can be installed first Baidu, very simple), the upper left corner

File-- > New Project

Then take a random name here. My name here is packages_demo.

In fact, whatever is said here, in fact, no matter what programming language, file, function naming are fastidious, try to take what you want to do to increase the readability of the code.

Then right-click on the newly created package, select Python File, and name it setup.py.

Then click the name of our project, select Directory, and create two new folders, one named packages_demo. The other is called demo.

Then under the packages_demo folder, create two new Python File, one called _ _ init__.py and the other called _ _ version__.py. By the way, build another Python File called firsrt.py. In addition, create a new Python File under the demo folder called outer.py.

Okay, now our engineering structure is as follows:

All right, let's stop for a moment and let's talk about the details.

When you first started learning Python, maybe you hit a print ("Hello World!") in Python Shell.

Later, you may also write a function in Shell. But when you turn off Shell, the code you write is gone, that is, it is not saved, and the problem arises. The code you write is not saved, and you can't use it later when you want to use it.

So you will choose to create a new file in Shell, then save it and name it demo.py. Then you can import the demo package you wrote in Shell, this package is the module,Python package in Python, including built-in packages and third-party packages, the built-in packages do not need to be installed, third-party packages need to be installed manually, and general packages can be installed through pip, then how do I know, those can use pip, those can not use pip? You install it, and if the installation is successful, it means you can.

Another way of installation is to download the source code of the package, and then execute python setup.py install, which can also be installed, and some packages have .whl suffix files, which can also be installed directly by pip xxx.whl install. Whl is actually a compiled pyd file, so there is no need to compile when it is installed locally. How is the pyd file generated? It is automatically generated by the Python interpreter when we compile the .py file.

One more thing to learn about the Python package is the following

If _ name__ = = "_ _ main__"

What does this sentence mean? When we import demo in Shell and calculate the sum of 1 ~ 2, the following sentence has no output. Then we Ctrl+F5, execute the demo.py file, and he outputs the result:

This means that the purpose of if _ _ name__ = = "_ _ main__" is that the following code runs only when demo.py is executed directly, but not when import is executed.

OK, in order for you to understand thoroughly, let's add a sentence to demo.py, print (_ _ name__)

Then import demo appears in Shell and the result is demo. Then call the _ _ name__ method of demo. The output is also demo.

Then run demo.py directly, with the explicit name _ _ main__,. You'll see.

At this point, that's all you need to know about module, and finally, the beginning of a double underscore in Python indicates a special method name. In other words, name is a special method in Python, so double underscores are added.

We can use the dir () method to see which methods are defined under the demo module.

You can see that in addition to add, there are several special methods, which are predefined by Python.

Then, notice that there is a special method of _ _ package__, which is called

The result shows that it is empty, which means there is no package. In fact, you can see here that package is one level higher than module.

Package in Python means that multiple module are made up of package.

There are two things to note about package: one is that each package needs to include a _ _ init__.py file under it, and the other is that package has the advantage of convenient code management and avoiding naming conflicts.

Okay, then let's go back to the file that we started to let you create. In fact, it is a new project, a new packages and a module under the project, one is called package_demo, the other is called demo. Then we write a little bit of code under first.py and outer.py:

First.py

Print ("This is first modlue")

Def fun (x):

Return x * 2

Outer.py

From packages_demo import first

Def fun ():

Return first.fun (Xero3)

If _ name__ = = "_ _ main__":

Results = fun ()

Print (results)

Then take a look at the setup.py file, here is to introduce the setuptools and the package, this is a Python built-in package management tool, you can compile, install, and manage Python packages. Here is a simple demo demonstration with very little code so that we can create our own package.

#! / usr/bin/env python

#-*-coding: utf-8-*-

From setuptools import find_packages, setup

NAME = 'packages_demo'

Setup (

Name=NAME

Version= "0.0.1"

Packages=find_packages ()

)

Then we open the Terminal command line under Pycharm and execute python setup.py install.

So we installed a package that we wrote ourselves. Isn't it handsome?

After installation, there are three more folders under the folder, and this is the compiled Python package.

Then we can also call it directly in our Shell, which shows that we have encapsulated a Python package, which is no different from the principle of peaceful os, sys, pandas, and so on.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report