我在使用Python 3.4,mongodb 4.2.10在Win 7(64)上运行pymongo时遇到问题.错误输出如下:
import pymongo
ImportError: No module named 'pymongo'
Run Code Online (Sandbox Code Playgroud)
代码非常简单:
import pymongo
from pymongo import MongoClient
client=MongoClient()
db=client.test_db
dict={'A':[1,2,3,4,5,6]}
db.test_collection.insert(dict)
to_print=db.test_collection.find()
print(to_print)
Run Code Online (Sandbox Code Playgroud)
我试过已经重新安装Python和MongoDB - 没有帮助.当我在cmd中手动执行它时,它工作,即mongod.exe和mongo.exe工作正常.似乎pymongo有问题,但我不知道如何解决它.
我正在使用 python 生成令牌作为安全措施。这是代码:
from PIL import Image, ImageDraw, ImageFont
image=Image.new("RGBA",(220,20),(255,255,255))
image_base=ImageDraw.Draw(image)
font = ImageFont.truetype("arial.ttf", 15)
font.size = 16
image_base.text((80,0),emailed_password,(0,0,0),font=font)
image_bytes=BytesIO()
image.save(image_bytes,format='png')
logo_file = open('static\images\RCI_logo.jpg', 'rb')
logo_bytes = logo_file.read()
logo_file.close()
token_name='registration_token'
secret_code_attachment = MIMEImage(image_bytes.getvalue(), name = '{0}.png'.format(token_name))
secret_code_attachment.add_header('Content-ID', '<{0}>'.format(token_name))
secret_code_attachment.add_header('Content-Disposition', 'inline')
message.attach(secret_code_attachment)
logo_file_name='logo_file_svg'
logo_attachment = MIMEImage(logo_bytes, name = '{0}.jpg'.format(logo_file_name))
logo_attachment.add_header('Content-ID', '<{0}>'.format(logo_file_name))
logo_attachment.add_header('Content-Disposition', 'inline')
message.attach(logo_attachment)
html = 'some message'
html_attachment = MIMEText(html, 'html')
message.attach(html_attachment)
smtpObj.sendmail(sender,receiver,message.as_string())
Run Code Online (Sandbox Code Playgroud)
该错误似乎是无法加载字体。虽然我是从 PIL 导入 ImageFont 。
错误信息如下:
ERROR in app: Exception on /registration [POST], referer: http://my.com/registration
Traceback (most …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用mongodb和pymongo运行Web应用程序以从数据库提供数据。
我得到的错误是ImportError:没有名为parse的模块。请参阅以下来自apache2 Web服务器的error.log:
mod_wsgi (pid=18824): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[:error] [pid 18824:tid 139967053518592] mod_wsgi (pid=18824): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[:error] [pid 18824:tid 139967053518592] Traceback (most recent call last):
File "/var/www/FlaskApp/flaskapp.wsgi", line 12, in <module>
[:error] [pid 18824:tid 139967053518592] from ABC import app as application
[:error] [pid 18824:tid 139967053518592] File "var/www/FlaskApp/ABC/__init__.py", line 1, in <module>
[:error] [pid 18824:tid 139967053518592] from pymongo import MongoClient
[:error] [pid 18824:tid 139967053518592] File "/var/www/FlaskApp/ABC/venv/lib/python3.4/site-packages/pymongo/__init__.py", line 92, in <module> …Run Code Online (Sandbox Code Playgroud) 我正在创建我想嵌入到电子邮件中的图像。我无法弄清楚如何将图像创建为二进制并传递到 MIMEImage。下面是我的代码,当我尝试读取图像对象时出现错误 - 错误是“AttributeError:'NoneType'对象没有属性'read'”。
image=Image.new("RGBA",(300,400),(255,255,255))
image_base=ImageDraw.Draw(image)
emailed_password_pic=image_base.text((150,200),emailed_password,(0,0,0))
imgObj=emailed_password_pic.read()
msg=MIMEMultipart()
html="""<p>Please finish registration <br/><img src="cid:image.jpg"></p>"""
img_file='image.jpg'
msgText = MIMEText(html,'html')
msgImg=MIMEImage(imgObj)
msgImg.add_header('Content-ID',img_file)
msg.attach(msgImg)
msg.attach(msgText)
Run Code Online (Sandbox Code Playgroud)
如果您查看第 4 行 - 我正在尝试读取图像,以便将其传递给 MIMEImage。显然,图像需要作为二进制读取。但是,我不知道如何将其转换为二进制以便 .read() 可以处理它。
FOLLOW-UP 我编辑从每建议jsbueno代码-非常感谢你!!!:
emailed_password=os.urandom(16)
image=Image.new("RGBA",(300,400),(255,255,255))
image_base=ImageDraw.Draw(image)
emailed_password_pic=image_base.text((150,200),emailed_password,(0,0,0))
stream_bytes=BytesIO()
image.save(stream_bytes,format='png')
stream_bytes.seek(0)
#in_memory_file=stream_bytes.getvalue()
#imgObj=in_memory_file.read()
imgObj=stream_bytes.read()
msg=MIMEMultipart()
sender='xxx@abc.com'
receiver='jjjj@gmail.com'
subject_header='Please use code provided in this e-mail to confirm your subscription.'
msg["To"]=receiver
msg["From"]=sender
msg["Subject"]=subject_header
html="""<p>Please finish registration by loging into your account and typing in code from this e-mail.<br/><img src="cid:image.png"></p>"""
img_file='image.png'
msgText=MIMEText(html,'html')
msgImg=MIMEImage(imgObj) #Is mistake …Run Code Online (Sandbox Code Playgroud) 我对将数据从数据库(mongodb)传递到html感到困惑。
我在“ init .py”中具有使用while逻辑查询数据库的python代码-参见以下代码段:
from pymongo import MongoClient
from flask import Flask, render_template
@app.route("/snapshot")
def pymongo_query():
db=client.collection
months=[1,2,3,4,5,6,7,8,9,10,11,12]
while months:
mon=months.pop()
query=list(db.collection.find({args}))
query=query[0]
month=query['month']
item_1=query['item_1']
item_2=query['item_2']
Run Code Online (Sandbox Code Playgroud)
该代码的作用是它遍历每个月,每个月由列表中的数字表示,并查询数据库中的各个月。我用while循环走了几个月。在Python中,我将在循环结束时使用print输出结果。
如何使用jinja2将数据输出到html中?我想知道我是否需要在jinja中使用while逻辑或将其保留在上面的init .py文件中。