2017-04-26 27 views
0

我正在使用peewee(ORM)創建一個小型Python-MySQL應用程序。 我的代碼完全在一個單一的文件,內容如下:如何在單獨的主python文件中鏈接多個python類?

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:[email protected]: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.pyusers.pyuserprofiles.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:[email protected]:3306/db') 

db.connect() 
Users.create(username = 'testname', password = '@[email protected]@', 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:[email protected]: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:[email protected]: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 

如何導入users.py和userprofiles.py在main.py執行使用peewee內main.py行動?

我想導入上面的py文件並通過鏈接兩個模型來執行數據庫操作。 我是一個編碼新手。開始使用Python的

+0

users.py和userprofiles.py都具有相同的內容。我相信這是一個複製/粘貼錯誤?另外,你在main.py中嘗試過了些什麼? – zmo

+0

@zmo這是一個錯誤。我會重新編輯我的問題。 – thepassionatecoder

+0

請參閱如何創建[mcve] –

回答

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:[email protected]:3306/db') 

    db.connect() 

    Users.create(username = 'testname', password = '@[email protected]@', 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,並在setup()函數中將that_pkg作爲包公開。

上面做了什麼?

我們已經創建了一個Python 稱爲that_pkg(Python包是在它一個__init__.py文件的目錄),其中包含幾個模塊:users.pyuserprofiles.pymain.py

前兩個模塊只是鬆散地描述你的模型,最後一個模塊主動實例化ORM並填充數據庫和其中的數據。

最後,爲了讓你的代碼更加乾淨,你應該使用create a setup.py file,並且爲了便於開發,使用virtualenv。

由於您是python包裝的新手,我的建議是開始使用pipenv這將幫助您維護像virtualenv這樣的開發所需的依賴關係,同時在pipfile中維護需求列表。 Nota Bene:當你剛剛接觸python時,不要開始使用python2.7進行學習,而是開始使用python3進行學習,python3現在已經在所有平臺上廣泛使用。

+0

謝謝。對於像我這樣的初學者來說簡潔。 – thepassionatecoder

相關問題