日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python 各層級目錄下的import方法

系統(tǒng) 1887 0

---恢復(fù)內(nèi)容開始---

以前經(jīng)常使用python2.現(xiàn)在很多東西都切換到了python3,發(fā)現(xiàn)很多東西還是存在一些差異化的??缒夸沬mport是常用的一種方法,并且有不同的表現(xiàn)形式,新手很容易搞混。有必要這里做個總結(jié),給大家科普一下:
1 同級目錄下的調(diào)用:
同級目錄下的調(diào)用比較簡單,一般使用場景是不同類的相互調(diào)用。不用考慮路徑問題,常用的格式是:from file import * 或者 from file import class/function 等。
下面以一個例子作為說明:
程序結(jié)構(gòu):
? dir_test git:(master) ? tree
.
├──? pycache
│?? └── test1.cpython-37.pyc
├── dir1
│?? └──?test3.py
├──?test1.py
└──?test2.py

代碼:

            
              from
            
             test1 
            
              import
            
             *

            
              #
            
            
               the below is also ok
            
            
              
#
            
            
              from test1 import dir_test
            
            
              def
            
            
               test_file2():
    
            
            
              print
            
            (
            
              "
            
            
              this is test file2
            
            
              "
            
            
              )

dir_test()
test_file2()
            
          

?

2 子目錄下的調(diào)用:
子目錄下的函數(shù)調(diào)用,正常的情況下,需要包含子目錄的,常用的格式如下:form dir1.file import * 或者: from dir1 import file等。
下面以一個例子說明:

? dir_test git:(master) ? tree
.
├──? pycache
│?? └── test1.cpython-37.pyc
├── dir1
│?? ├──? pycache
│?? │?? └── test3.cpython-37.pyc
│?? └──?test3.py
├──?test1.py
└──?test2.py

代碼:

            
              from
            
             test1 
            
              import
            
             *

            
              #
            
            
               the below is also ok
            
            
              
#
            
            
              from test1 import dir_test
            
            
              from
            
             dir1.test3 
            
              import
            
             *


            
              def
            
            
               test_file2():
    
            
            
              print
            
            (
            
              "
            
            
              this is test file2
            
            
              "
            
            
              )

dir_test()
dir1_test()
            
          

?

3 上級目錄下的調(diào)用:
上級目錄調(diào)用要比上兩種復(fù)雜,這里要用到sys函數(shù),首先要在將要調(diào)用的文件下面建一個空文件: init .py 然后在調(diào)用這個文件的文件里面添加:sys.path.append("…"),才可以調(diào)用成功:
下面是一個例子:文件結(jié)構(gòu):
? dir_test git:(master) ? tree
.
├──? pycache
│?? └── test1.cpython-37.pyc
├── dir1
│?? ├──? init .py
│?? ├──? pycache
│?? │?? ├──? init .cpython-37.pyc
│?? │?? └── test3.cpython-37.pyc
│?? └──?test3.py
├── dir2
│?? └──?test4.py
├──?test1.py
└──?test2.py
代碼:

            
              #
            
            
              !python3
            
            
              import
            
            
               sys
sys.path.append(
            
            
              "
            
            
              ..
            
            
              "
            
            
              )

            
            
              from
            
             dir1.test3 
            
              import
            
             *

            
              #
            
            
              import dir1
            
          

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 陵川县| 绿春县| 长宁区| 太谷县| 凤山市| 无极县| 武乡县| 平谷区| 万源市| 罗城| 秭归县| 禹城市| 化隆| 广丰县| 莲花县| 渭源县| 青田县| 布拖县| 乌兰察布市| 株洲县| 襄汾县| 遂昌县| 仁布县| 玛沁县| 翁牛特旗| 来安县| 富锦市| 东港市| 辽阳市| 土默特左旗| 秦安县| 卓尼县| 兴文县| 方城县| 崇阳县| 青冈县| 甘洛县| 吴江市| 松桃| 周口市| 维西|