What are the ways to import python packages?

WBOY
Release: 2023-04-18 21:16:03
forward
4554 people have browsed it

The execution file and the target import module are in the same directory

Direct import

For example, I want to import env package in ma_main.py The make_env.py file, thereby reading the functions

because at this time ma_main.py and env package are both in src is in the root directory, so you can directly import and continue to import

What are the ways to import python packages?

herefromand thenimportbecause## There is only one function in #make_env.py which is make_env(), so import this function directly and write the name directly when calling

What are the ways to import python packages?

If it is

from env import make_env, then when calling the make_env() function in make_env.py, you must also use . Statement

PS: from env import make_env and import env.make_env are the same

execution file and target import The module is not in the same directory

If the executable file and the module are not in the same directory, then direct import will

not find the custom module.

What are the ways to import python packages?

What are the ways to import python packages?

For example: To import the

pwcong package in main.py Thus executing some methods of __init__.py

However,

main.py is under ./Torch/main/, and pwcong Under ./Torch/

is not in the same directory. At this time,

main.pyfilelist of paths searched by the python interpreter module does not include ./Torch

, so at this time you need to use the

sys package that comes with python to import the path of the custom module , thereby manually adding the path list of the module search by the Python interpreter in main.py, and then the

Python interpreter search module can be successfully imported. Path List

  • In Python, when you

    import a module, the interpreter will find the module from a path list.

  • This path list usually includes the

    default path for the Python interpreter installation , as well as any paths specified in the environment variable PYTHONPATH .

  • Use the

    sys.path.append() function to add a new path to this path list so that the interpreter can find you specified module.

  • After adding a new path, you can import the module under the path through the

    import statement.

  • This does

    not affect the namespace , but just tells the interpreter where to find the module to be imported.

Operation steps

The steps to import a custom module are as follows:

  • First import the sys module

  • Then import the directory where the

    custom module is located through the sys.path.append(path) function

  • Import Custom modules.

That is, write the following code in the top few lines of

main.py to manually define the python interpreter path of the file

import sys
sys.path.append(r"C:\Users\PC\Desktop\Torch") # 要加上地址生命符r, 并且要是绝对路径
Copy after login

In# Define a hello function in ##__init__.py

, print hello

What are the ways to import python packages?Add the path searched by the interpreter module through sys.path.append, and then execute it through the module name Function

What are the ways to import python packages?A module must have a

__init__.py

fileIn Python,

a directory

requires To be considered a package, it must contain an __init__.py file. This file can be an empty file, or contain some package initialization code. A directory without a

__init__.py

file is just a directory, and other py files cannot import the functions

__init__ in any py file in this directory. py

The file may contain:

  • Declaration namespace

    : __init__.pyThe file may contain declarations of variables, classes, and functions

  • Initialization code

    : The init.py file can contain initialization code that will be executed when the package is imported. This can be used to perform certain necessary operations, such as configuring a package or ensuring that a package's dependencies are installed.

  • Submodule import

    : The init.py file can contain code that imports other modules. This can be used to add submodules to a package's namespace so that users can access them.

Namespace

In Python, a namespace is a dictionary containing variable and function names, used to record the name and value of each identifier (identifier). Each namespace in the Python interpreter is a dictionary object whose variables and functions can be accessed through the dictionary's key-value pairs.

There are three namespaces in Python:

  • Built-in namespace: Contains the variables and functions built into the Python interpreter. These variables and functions It can be used directly in the program without importing any modules, such as: print() function, len() function, etc.

  • Global namespace: Created when the module is defined, it contains variables and functions defined in the module file. These variables and functions can be used anywhere in the module.

  • Local namespace: Created when a function is called, it contains variables and functions defined in the function. These variables and functions can only be used inside functions.

The role of namespace is to avoid variable or function name conflicts between different namespaces and to better manage variables and functions in the program. In Python, variables in the global and non-local namespaces can be accessed and modified by using the global and nonlocal keywords.

The above is the detailed content of What are the ways to import python packages?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!