Home >Backend Development >Python Tutorial >Import into the upper-level directory module and circular import issues in Python

Import into the upper-level directory module and circular import issues in Python

高洛峰
高洛峰Original
2017-03-03 15:20:442003browse

Import modules in the upper-level directory

In python, the import module will search for sys.path. sys.path is a list, and we can modify it dynamically.
To import a module in a certain directory, we use sys.path.insert(0,somedir) to add the search path and then import.
In this case, if you want to import the module in the upper-level directory, you can use sys.path.insert(0,parentdir).
However, this method of writing absolute paths will not work if the file is placed elsewhere.
So use dynamic method to get the upper level directory.

import os,sys 
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
sys.path.insert(0,parentdir)

Why use sys.path.insert(0,parentdir) instead of sys.path.append(parentdir)
Because The search path is traversed, so if there is a module with the same name in other paths, an import error will occur. Use sys.path.insert(0,parentdir) to ensure that this path is searched first.

Solving the problem of circular import
In python, we often encounter the problem of circular import, that is, circular import.
This funny situation often occurs in reality.
When installing a wireless network card, you need to go online to download the network card driver..
When installing compression software, the compression software installation program downloaded from the Internet is actually compressed Yes..
Circular dependency is similar to this situation.
For example,
is in models.py,

from server import db 
class User(db.Model): 
  pass

is in server.py,

from flask import Flask 
from flask.ext.sqlalchemy import SQLAlchemy 
app = Flask(__name__) 
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' 
db = SQLAlchemy(app) 
from models import User

This creates the problem of circular import.
There are several main ways to solve the problem of circular import.
1. Lazy import
Write the import statement in a method or function to limit its scope to a local area.
The disadvantage of this method is that there will be performance issues.
2. Change from xxx import yyy to import xxx;xxx.yyy to access the form
3. Organize the code
The problem of circular import often means that there is a problem with the layout of the code.
You can merge or separate competing resources.
If you merge, you will write them all into one file.
Separation is to extract the resources that need to be imported into a third-party file.
In short, it is to turn the loop into one direction.

For more articles related to import into the upper-level directory module and circular import issues in Python, please pay attention to the PHP Chinese website!

Statement:
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