pythonモジュールインポート
2481 ワード
1、モジュールの定義:
2、インポート方法:
3、import本質(経路探索と探索経路)
4、インポートの最適化例:
: python ( 、 、 、 ; : ), .py python 。
:
: , ( __init__.py )
2、インポート方法:
:
1.import module_name ;
module_name.name
2.import module_name,module2_name
3.from module_name import * module_name , !
4.from module_name import name module_name name ;
name
import from
1.import module_name.logger()
2.from logger() ;from , 。
: , 。
:
import
:
:
import sys
sys.path
import os
os.path.abspath(__file__)
abspath()
__file__
os.path.dirname(os.path.abspath(__file__))
dirname()
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append() python , 。
sys.path.insert() python 。
:
import sys,os
print(sys.path)
a = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(a)
print(sys.path)
from xxx import xxx
3、import本質(経路探索と探索経路)
: python
:import module_name - - -》 module_name.py- - -》 module_name.py - - -》 sys.path, 。
: __init__.py
test1.py
1. __init__.py , py
from . imprt test1
#import test1
2. py test1.py
4、インポートの最適化例:
:
module_test.py
def test():
print ("in the test!")
test.py
import module_test
def logger():
module_test.test()
print("in the logger!")
def search():
module_test.test()
print("in the search!")
:
module_test.py
def test():
print ("in the test!")
test.py
from module_test import test
def logger():
test()
print("in the logger!")
def search():
test()
print("in the search!")
:
, :
from module_test import test as test01