Learning python, day5: definition module are introduced,

1. Definitions

Module: used to logically organize PYTHON code (variable, function, class, logic: the realization of a function), essentially. The end of the py python file (file name: test.py, the corresponding module name is: test)

Package: essentially a directory (you must bring a file __init__.py of)

2, introduction method

import module_name

import module_name,module_name2

from module_name import * # do not recommend using all modules of code substitution, easily conflict with the same name

from module_name import logger as logger_alex # a nickname, to avoid conflict in the module program

Module module alex calls

# coding=utf-8
# Author: RyAn Bi
name= 'alex'
def say_hello():
    print('hello')

def logger():
    print('im logger in module')

 

The main program

# coding=utf-8
# Author: RyAn Bi

#import module_alex
from module_alex import name,logger as logger_alex


def logger():
    print('im logger in main')

logger()

logger_alex()
print(name)   #可以导入参数

3, the nature of the import (route search and search path)

Import module is essentially the python file to explain it (import test test = 'test.py all code')

(from test import ml ml = “code”)

Import nature of the package, is to explain bag init file

import module_name -----> module_name.py ----> path of module_name.py ---> new path, adding environment variables (sys.path)

 

SYS Import, os 

Print (sys.path) # is a list of paths 

path print (os.path.abspath (__ file__)) # current directory 
print (os.path.dirname (os.path.dirname (os.path. abspath (__ file__)))) # return to the parent directory is the parent directory of the calling module 
sys.path.append (os.path.dirname (os.path.dirname (os.path.abspath ( __ file__)))) # the new directory is added to the environment variable 
Import AS module_alex the X- 


x.say_hello

Guess you like

Origin www.cnblogs.com/bbgoal/p/11352762.html