Python learning module loading for -3.python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.