Home > Backend Development > Python Tutorial > How to call each other between python files written by yourself

How to call each other between python files written by yourself

anonymity
Release: 2019-06-17 16:03:53
Original
10163 people have browsed it

How do python files written by myself call each other? Module libraries in Python are very commonly used. You can customize commonly used modules by yourself, but how to call them?

How to call each other between python files written by yourself

Modules call each other

Two methods when calling the same level directory

 import module
 print(module.add(3,8))
 
 from module import add
 print(add(2,4))
Copy after login

Subdirectory calls of the same level directory

 from 文件相互调用 import cal
 print(cal.sub(3,8))
 from 文件相互调用.cal import  sub    #这种情况下加一个点(.),然后写目录
 print(sub(3,9))
Copy after login

Subdirectory calls of the subdirectories of the same level directory

 from 文件相互调用.文件2 import cal2
 print(cal2.mul(3,7))
 
 from 文件相互调用.文件2.cal2 import mul
 print(mul(3,7))
Copy after login

Go deeper one level in turn

from 文件相互调用.文件2.文件3 import cal3
print(cal3.divi(8,2))
from 文件相互调用.文件2.文件3.cal3 import  divi
print(divi(8,2))
Copy after login

Special Situation

from 文件相互调用.文件2 import 文件3    #这种不推荐用这种的
print(文件3.cal3.divi(8,2))
Copy after login

There is a special calling format, but it has two shortcomings:

First, the execution efficiency is low; secondly, it cannot determine whether the file is duplicated, etc.

from 文件相互调用 import *
print(cal.sub(3,8))
Copy after login
from 文件相互调用.文件2.文件3 import *
print(cal3.divi(8,2))
Copy after login

When calling multiple modules at the same time, the abbreviation can be as follows

import xx, xx,xx, ...   #即用逗号隔开
Copy after login

When import calls a module, it does two things:

1. The called module will be executed once

2 ,Introduce variable names (i.e. module names)

Normally, all programs are not written in one file, but written in different files according to functions

For example:

bin文件时程序的的入口
main 文件里只写逻辑程序
module 文件里面只写功能程序
Copy after login

Function of the package:

1. Used to organize modules, which can be sub-packaged according to the function of the module;

2.To avoid module conflicts; and with the same name If the modules are placed in different packages, there will be no conflicts.

if __name__ == "__main__":
    print(add(3,5))
    print('ok')
print(__name__)
Copy after login

if __name__ == "__main__": has two functions:

1. Put a certain file to test the result of the file. However, when the file is called, The statement will not be executed.

To put it simply: for testing the called file

2. Write this sentence in the calling program to prevent others from modifying my main program, which is only for others to call.

The above is the detailed content of How to call each other between python files written by yourself. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template