Home  >  Q&A  >  body text

python - from ..xxxx import xxxx到底是什么意思呢?

flaskweb开发书中:

  1 from flask import render_template, redirect, request, url_for, flash
  2 from flask_login import login_user, logout_user, login_required,current_user
  3 from . import auth
  4 from .. import db
  5 from ..models import User
  6 from ..email import send_email
  7 from .forms import LoginForm,RegistrationForm

上述.和..起到什么作用呢?
tree是这样的

├── app
│   ├── auth
│   │   ├── forms.py
│   │   ├── forms.pyc
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── views.py
│   │   └── views.pyc
│   ├── email.py
│   ├── email.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── main
│   │   ├── errors.py
│   │   ├── errors.pyc
│   │   ├── forms.py
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── views.py
│   │   └── views.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── static
│   │   └── favicon.ico
│   └── templates
│       ├── 404.html
│       ├── 500.html
│       ├── auth
│       │   ├── email
│       │   │   ├── confirm.html
│       │   │   └── confirm.txt
│       │   ├── login.html
│       │   ├── register.html
│       │   └── unconfirmed.html
│       ├── base.html
│       ├── index.html
│       └── mail
│           ├── new_user.html
│           └── new_user.txt
├── config.py
├── config.pyc
├── LICENSE
├── manage.py
├── README.md
├── requirements.txt
└── tests
    ├── __init__.py
    ├── test_basics.py
    └── test_user_model.py

这个脚本在app/auth/下

黄舟黄舟2741 days ago930

reply all(4)I'll reply

  • 怪我咯

    怪我咯2017-04-18 10:35:04

    .. and . are the meaning of this directory and the superior directory. You must be able to use cd .. right

    ❯ ls -al
    total 1660
    drwxr-xr-x+ 189 caimaoy staff   6426  4 11 10:07  .
    drwxr-xr-x    5 root    admin    170 12  7  2015  ..

    Writing like this in python also means the same thing, take

    from ..models import User

    As an example

    Compared to auth, models can only be found by going back to the upper layer first.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:35:04

    from . is to search for module files from the directory where the current file is located,
    from .. is the directory superior to the directory where the current file is located.

    Refer to the official instructions here: https://docs.python.org/2/tut...

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:35:04

    from xx import xxx
    import xx
    Python uses this import module. The module can be a function, class, or collection.

    This method is mainly to differentiate naming. If the called module function names are repeated, they can be distinguished.

    import xx calls the entire package.
    from xx import xxx calls a function in the package.

    For example:
    I want a school bag import bag
    I want a book in the school bag from bag import book

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:35:04

    The answer you want is not here. Look for a basic Python book and read carefully about module import.

    reply
    0
  • Cancelreply