How to use import in python

coldplay.xixi
Release: 2023-01-03 09:28:04
Original
26023 people have browsed it

Use the import method in python: 1. [import module_name], that is, the module name is directly connected after import; 2. [from package_name import module_name] is a collection of modules.

How to use import in python

The operating environment of this tutorial: Windows 7 system, python version 3.9, DELL G3 computer.

Use the import method in python:

First, create a folder Tree as the working directory, and create two files m1.py and m2 in it .py, write code in m1.py:

import os
import m2
m2.printSelf()
Copy after login

Write code in m2.py:

def printSelf():
print('In m2')
Copy after login

Open the command line, enter the Tree directory, type python m1.py and run , found that no error was reported, and In m2 was printed, indicating that there is no problem using import in this way. From this we summarize the first usage of the import statement.

<strong>import module_name</strong>. That is, the module name is directly connected after import. In this case, Python will look for this module in two places. The first is sys.path (check it by running the code import sys; print(sys.path)). The directory where the os module is located is in the list sys.path , the directories of generally installed Python libraries can be found in sys.path (the premise is to add the Python installation directory to the computer's environment variables), so for the installed libraries, we can directly import them. The second place is the directory where the running file (here, m1.py) is located. Because m2.py and the running file are in the same directory, there is no problem with the above writing method.

There is no problem in importing the library in the original sys.path using the above method. However, it is best not to use the above method to import files in the same directory! Because this can go wrong. To demonstrate this error, you need to use the second way of writing the import statement, so let's learn the second way of writing import first. Create a new directory Branch under the Tree directory, and create a new file m3.py in Branch. The content of m3.py is as follows:

def printSelf():
print(&#39;In m3&#39;)
Copy after login

How to import m3.py into m1, please see the changed m1.py :

from Branch import m3
m3.printSelf()
Copy after login

Summarize the second usage of the import statement:

##from package_name import module_name<strong></strong>. Generally, a collection of modules is called a package. Similar to the first way of writing, Python will look for the package in two places: sys.path and the running file directory, and then import the module named module_name in the package.

Now let’s explain why you should not use the first way of import to import files in the same directory. Create a new m4.py file in the Branch directory. The content of m4.py is as follows:

def printSelf():
print(&#39;In m4&#39;)
Copy after login

Then we directly import m4 in m3.py, and m3.py becomes:

import m4
def printSelf():
print(&#39;In m3&#39;)
Copy after login

At this time When running m1.py, an error will be reported, saying that the m4 module cannot be imported. why? Let's take a look at the import process: m1 uses from Branch import m3 to import m3, and then uses import m4 in m3.py to import m4. See the problem? m4.py and m1.py are not in the same directory. How can I directly use import m4 to import m4. (Readers can try to create another m4.py file directly in the Tree directory. You will find that there will be no error when running m1.py again, but the second m4.py is imported)

Faced with the above error, no error will be reported when running m1.py using python2, because in python2, the two ways of writing import mentioned above are relative imports, while in python3, they are absolute imports. Having said that, it comes to the most critical part of import - relative import and absolute import.

We still talk about the import usage of python3. The two writing methods mentioned above are absolute imports, which are used to import packages in sys.path and packages in the directory where the running file is located. For packages in sys.path, this way of writing is no problem; if you import a file you wrote yourself, if it is a non-running entry file (the m1.py above is a running entry file, you can use absolute import), you need to import it relatively.

For example, for the non-running entry file m3.py, you need to use relative import to import m4.py:

from . import m4
def printSelf():
print(&#39;In m3&#39;)
Copy after login
It will be ok to run m1.py at this time.

Related free learning recommendations:

python video tutorial

The above is the detailed content of How to use import in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template