【问题标题】:How to link multiple python classes in a separate main python file?如何在一个单独的主 python 文件中链接多个 python 类?
【发布时间】:2017-09-23 21:59:34
【问题描述】:

我正在使用 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")

用户和用户配置文件是使用 peewee 定义的模型。我可以在单个文件中使用这些模型创建条目。

现在我尝试将其拆分为 3 个文件:ma​​in.py、users.py、userprofiles.py main.py - 应该调用 users.py 和 userprofiles.py 的主文件

我的ma​​in.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 文件并通过链接两个模型来执行 db 操作。 我是编码新手。 Python 入门

【问题讨论】:

  • users.py 和 userprofiles.py 的内容相同。我相信这是复制/粘贴错误?另外,到目前为止,您在 main.py 中尝试过什么?
  • @zmo 这是一个错误。我将重新编辑我的问题。
  • 查看如何创建minimal reproducible example

标签: python python-2.7 class python-module peewee


【解决方案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 文件的目录),其中包含几个模块:users.py、userprofiles.py 和main.py.

前两个模块只是粗略地描述您的模型,最后一个模块是主动实例化 ORM 并填充数据库和其中的数据。

最后,为了让你的代码漂亮干净,你应该create a setup.py file,并且为了便于开发使用virtualenv。

由于您是 python 打包的新手,我对您的建议是开始使用 pipenv,这将帮助您维护开发所需的依赖项,例如 virtualenv,同时维护 pipfile 中的需求列表。

Nota Bene:因为你是 python 新手,所以不要从 python2.7 开始学习,从现在在所有平台上广泛使用的 python3 开始学习。

【讨论】:

  • 谢谢。对于像我这样的初学者来说,它很简洁。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-28
  • 2014-06-19
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
相关资源
最近更新 更多