当我尝试导入passlib时出现以下错误。
ImportError: No module named passlib.apps
我试图通过以下命令安装它来安装它并声称它已成功或已经存在但我的程序仍然拒绝导入它:
pip install passlib
sudo pip install passlib
apt-get install python-pip pip install passlib
以下是我构建环境的方法:
我导航到Documents / 45
我运行命令:source env / bin / activate
然后我运行pip install passlib,它告诉我:
要求已经满足:passlib in ./env/lib/python2.7/site-packages
(env) jsnyder10@jsnyder10-VirtualBox:~/Documents/45$ ./run.py
Traceback (most recent call last):
File "./run.py", line 2, in <module>
from app import app
File "/home/jsnyder10/Documents/45/app/__init__.py", line 70, in <module>
from app import views, models
File "/home/jsnyder10/Documents/45/app/views.py", line 9, in <module>
from .forms import EditForm, PostForm, SearchForm, RegistrationForm, LoginForm
File "/home/jsnyder10/Documents/45/app/forms.py", line 5, in <module>
from .models import User
File "/home/jsnyder10/Documents/45/app/models.py", line 6, in <module>
from passlib.apps import custom_app_context as pwd_context
ImportError: No module named passlib.apps
(env) jsnyder10@jsnyder10-VirtualBox:~/Documents/45$ pip install passlib
Requirement already satisfied: passlib in ./env/lib/python2.7/site-packages
这是我的代码 run.py
#!flask/bin/python
from app import app
app.run(debug=True)
这是 init .py
init__.py
import os
from flask import Flask
from flask.json import JSONEncoder
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_mail import Mail
from flask_babel import Babel, lazy_gettext
from config import basedir, ADMINS, MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, \
MAIL_PASSWORD
from .momentjs import momentjs
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
lm = LoginManager()
lm.init_app(app)
lm.login_view = 'login'
lm.login_message = lazy_gettext('Please log in to access this page.')
mail = Mail(app)
babel = Babel(app)
class CustomJSONEncoder(JSONEncoder):
"""This class adds support for lazy translation texts to Flask's
JSON encoder. This is necessary when flashing translated texts."""
def default(self, obj):
from speaklater import is_lazy_string
if is_lazy_string(obj):
try:
return unicode(obj) # python 2
except NameError:
return str(obj) # python 3
return super(CustomJSONEncoder, self).default(obj)
app.json_encoder = CustomJSONEncoder
if not app.debug and MAIL_SERVER != '':
import logging
from logging.handlers import SMTPHandler
credentials = None
if MAIL_USERNAME or MAIL_PASSWORD:
credentials = (MAIL_USERNAME, MAIL_PASSWORD)
mail_handler = SMTPHandler((MAIL_SERVER, MAIL_PORT),
'no-reply@' + MAIL_SERVER, ADMINS,
'microblog failure', credentials)
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
if not app.debug and os.environ.get('HEROKU') is None:
import logging
from logging.handlers import RotatingFileHandler
file_handler = RotatingFileHandler('tmp/microblog.log', 'a',
1 * 1024 * 1024, 10)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(logging.Formatter(
'%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO)
app.logger.info('microblog startup')
if os.environ.get('HEROKU') is not None:
import logging
stream_handler = logging.StreamHandler()
app.logger.addHandler(stream_handler)
app.logger.setLevel(logging.INFO)
app.logger.info('microblog startup')
app.jinja_env.globals['momentjs'] = momentjs
from app import views, models
models.py
from hashlib import md5
import re
from app import db
from app import app
from config import WHOOSH_ENABLED
from passlib.apps import custom_app_context as pwd_context
点子列表和冻结输出,均显示passlib 1.7.1
(env) jsnyder10@jsnyder10-VirtualBox:~/Documents/45$ pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
appdirs (1.4.3)
packaging (16.8)
passlib (1.7.1)
pip (9.0.1)
pyparsing (2.2.0)
setuptools (35.0.2)
six (1.10.0)
wheel (0.29.0)
(env) jsnyder10@jsnyder10-VirtualBox:~/Documents/45$ pip freeze
appdirs==1.4.3
packaging==16.8
passlib==1.7.1
pyparsing==2.2.0
six==1.10.0
答案 0 :(得分:0)
但它对我有用,所以问题可能在你的环境中。
- 我正在使用Python 2.7并安装(通过pip)passlib-1.7.1 .-
我没有意识到我正在从flask / bin中运行环境,因此运行以下命令修复它
flask/bin/pip install passlib