您的位置:首頁技術文章
文章詳情頁

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

瀏覽:146日期:2022-08-02 16:38:15

問題描述

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/下

問題解答

回答1:

.. 和 . 就是這個本目錄和上級目錄的意思,你一定會用 cd .. 吧

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

python 里面這樣寫也是一個意思,拿

from ..models import User

作為例子

models 相對于 auth 就是要先回到上層才能找到。

回答2:

from . 是從當前文件所在的目錄下尋找模塊文件,from .. 就是當前文件所在的目錄上級目錄。

參考官方說明在這:https://docs.python.org/2/tut...

回答3:

from xx import xxximport xxpython使用這個導入模塊,模塊可以是函數,類,集合。

這種方式主要是區別命名,如果調用的模塊函數名重復,可以加以區分。

import xx 調用整個包。from xx import xxx 調用包里某個函數。

比如:我要書包 import bag我要書包里的書 from bag import book

回答4:

你想要的答案不在這里,認真找本python基礎的書籍,關于模塊導入的內容好好看看。

標簽: Python 編程
相關文章:
国产综合久久一区二区三区