我正在使用peewee(ORM)创建一个小型的Python-MySQL应用程序。 我的代码在单个文件中完美运行,如下所示:
import os
from peewee import *
from playhouse.db_url import connect
# Connect to the database URL defined in the environment, falling
# back to a local MySql database if no database URL is specified.
db = connect(os.environ.get('DATABASE') or 'mysql://testdb:testdb@127.0.0.1:3306/db')
db.connect()
class Users(Model):
users_id = PrimaryKeyField()
username = CharField()
password = CharField()
mobile_number = CharField()
created_at = DateTimeField()
updated_at = DateTimeField()
class Meta:
database = db
class User_profiles(Model):
users_id = IntegerField()
user_profiles_id = PrimaryKeyField()
profile_name = CharField()
address = CharField()
created_at = DateTimeField()
updated_at = DateTimeField()
class Meta:
database = db
Users.create(username = "Adam", password = "Dummy1", mobile_number = "1234567891")
User_profiles.create(users_id=4,profile_name="shop", address="Delhi")
用户& Userprofiles是使用peewee定义的模型。我能够在单个文件中使用这些模型创建条目。
现在我尝试将其拆分为3个文件: main.py , users.py , userprofiles.py main.py - 应该调用users.py和userprofiles.py
的主文件我的 main.py
import os
from peewee import *
from playhouse.db_url import connect
# Connect to the database URL defined in the environment, falling
# back to a local MySql database if no database URL is specified.
db = connect(os.environ.get('DATABASE') or 'mysql://testdb:testdb@127.0.0.1:3306/db')
db.connect()
Users.create(username = 'testname', password = '@88@@', mobile_number='1234567811'):
我的 users.py :
import os
from peewee import *
from playhouse.db_url import connect
# Connect to the database URL defined in the environment, falling
# back to a local MySql database if no database URL is specified.
# db = connect(os.environ.get('DATABASE') or 'mysql://testdb:testdb@127.0.0.1:3306/db')
db.connect()
class Users(Model):
users_id = PrimaryKeyField()
username = CharField()
password = CharField()
mobile_number = CharField()
created_at = DateTimeField()
updated_at = DateTimeField()
class Meta:
database = db
我的 userprofiles.py :
import os
from peewee import *
from playhouse.db_url import connect
# Connect to the database URL defined in the environment, falling
# back to a local MySql database if no database URL is specified.
# db = connect(os.environ.get('DATABASE') or 'mysql://testdb:testdb@127.0.0.1:3306/db')
# db.connect()
class User_profiles(Model):
users_id = IntegerField()
user_profiles_id = PrimaryKeyField()
profile_name = CharField()
address = CharField()
created_at = DateTimeField()
updated_at = DateTimeField()
class Meta:
database = db
如何在main.py中导入users.py和userprofiles.py以使用peewee在main.py中执行操作?
我试图通过链接两个模型来导入上面的py文件并执行数据库操作。 我是编码的新手。 Python入门
答案 0 :(得分:1)
创建一个目录,并创建一个空文件__init__.py
:
mkdir that_pkg
touch that_pkg/__init__.py # unix command to create an empty file
所以你创建了一个新的python模块that_pkg
。
然后创建that_module/users.py
:
# always make explicit includes
from peewee import Model, PrimaryKeyField, CharField, DateTimeField
class Users(Model):
users_id = PrimaryKeyField()
username = CharField()
password = CharField()
mobile_number = CharField()
created_at = DateTimeField()
updated_at = DateTimeField()
class Meta:
database = db
然后创建that_pkg/userprofiles.py
:
from peewee import Model, PrimaryKeyField, CharField, DateTimeField
class User_profiles(Model):
users_id = IntegerField()
user_profiles_id = PrimaryKeyField()
profile_name = CharField()
address = CharField()
created_at = DateTimeField()
updated_at = DateTimeField()
class Meta:
database = db
最后创建that_pkg/main.py
:
import os
from playhouse.db_url import connect
from that_pkg.users import User
from that_pkg.userprofiles import User_profiles
def main():
db = connect(os.environ.get('DATABASE') or 'mysql://testdb:testdb@127.0.0.1:3306/db')
db.connect()
Users.create(username = 'testname', password = '@88@@', mobile_number='1234567811')
# code that will be executed when you run this file directly:
if __name__ == "__main__":
main()
最后,您可以使用以下命令执行代码:
python that_pkg/main.py
您还可以创建一个setup.py,并在that_pkg
函数中将setup()
公开为包。
上面做了什么?
我们创建了一个名为that_pkg
的python 包(一个python包是一个包含__init__.py
文件的目录),其中包含几个模块:{{1} },users.py
和userprofiles.py
。
前两个模块只是松散地描述了你的模型,最后一个模块正在积极地实例化ORM并填充数据库及其中的数据。
最后,为了使您的代码更加干净,您应该create a setup.py
file,并且为了便于开发,请使用virtualenv。
由于您是python包装的新手,我的建议是开始使用pipenv
,它将帮助您维护开发所需的依赖关系,如virtualenv,同时维护pipfile中的需求列表。
Nota Bene:因为你是python的新手,不要开始学习python2.7,开始学习python3,现在可以在所有平台上广泛使用。