The import keyword is used in Python for module loading.
Create the Pythonmoduleload project as a demonstration in Visual Studio first.
1, the same directory loading
Create a samefolder.py file
Write code:
1 def Printsamefolder (): 2 Print ("This method was in thesame folder")
Modify startup file, default to pythonmoduleload.py
1 Import Samefolder 2 Samefolder.printsamefolder ()
Note that after writing to import, Samefolder has been only perceived by Visual Studio, and PTVS (Python Tools for Visual Studio) is a good feature.
2. Sub-Directory Loading
Create a Otherfolder folder and add otherfolder.py
Write code:
1 def Printotherfolder (): 2 Print ("This method was in the otherfolder")
Modify the Startup file
1 Import Otherfolder.otherfolder 2 OtherFolder.OtherFolder.printOtherFolder ()
This explains that the first otherfolder of import in the first row refers to the Otherfolder folder, and then the second one refers to the otherfolder.py file. The second line, in the same vein, calls the Printotherfolder function in the otherfolder.py in the Otherfolder folder.
In this case, Visual Studio's smart tips don't.
3. Append directory Loading
Modify the Startup file
1 Import SYS 2 sys.path.append ("./otherfolder/")34import Otherfolder5 otherfolder.printotherfolder ()
First introduce SYS this Python system package, and then call the Sys.path.append method, add otherfolder This directory path, and then the same directory, loading module on the line, this time PTVs syntax hints also normal work.
Python learning module loading for -3.python