1. 程式人生 > >python模組匯入以及sys庫的新增路徑使用

python模組匯入以及sys庫的新增路徑使用

整體檔案的路徑是這樣的:execute是我當前執行程式的模組

模組匯入:

1.如果你要匯入的模組或者資料夾下的模組與你要執行程式的模組在同一個目錄下

檔案:import  second_module

資料夾:from  second_file   import  second_file_moudle

2.如果你要匯入的檔案或者資料夾下的模組,可以用sys來臨時匯入模組路徑:

匯入路徑:import  sys

                   sys.path.append(模組的絕對路徑)

例如:sys.path.append(r'User/jing/PycharmProject/import_study/first')

            sys.path.append(r'User/jing/PycharmProject/import_study/first/first_file')

注意:1.絕對路徑寫到模組所在的資料夾即可。

重要提示:如果你要匯入的模組或者資料夾下的模組與你要執行程式的模組在同一個目錄下時,是不用sys的,如果你想要匯入資料夾下的模組時用sys是沒用的。

例如:import  sys

            sys.path.append(r'User/jing/PycharmProject/import_study/second/second_file')

            import  second_file_module

是會報找不到second_file_module的。