pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<small id='EOzHP'></small><noframes id='EOzHP'>

    <bdo id='EOzHP'></bdo><ul id='EOzHP'></ul>

    <i id='EOzHP'><tr id='EOzHP'><dt id='EOzHP'><q id='EOzHP'><span id='EOzHP'><b id='EOzHP'><form id='EOzHP'><ins id='EOzHP'></ins><ul id='EOzHP'></ul><sub id='EOzHP'></sub></form><legend id='EOzHP'></legend><bdo id='EOzHP'><pre id='EOzHP'><center id='EOzHP'></center></pre></bdo></b><th id='EOzHP'></th></span></q></dt></tr></i><div class="d7t5p5f" id='EOzHP'><tfoot id='EOzHP'></tfoot><dl id='EOzHP'><fieldset id='EOzHP'></fieldset></dl></div>
    1. <tfoot id='EOzHP'></tfoot>
      <legend id='EOzHP'><style id='EOzHP'><dir id='EOzHP'><q id='EOzHP'></q></dir></style></legend>

      python:不同包下同名的兩個模塊和類

      python: Two modules and classes with the same name under different packages(python:不同包下同名的兩個模塊和類)

      <small id='xoair'></small><noframes id='xoair'>

        <bdo id='xoair'></bdo><ul id='xoair'></ul>
        <i id='xoair'><tr id='xoair'><dt id='xoair'><q id='xoair'><span id='xoair'><b id='xoair'><form id='xoair'><ins id='xoair'></ins><ul id='xoair'></ul><sub id='xoair'></sub></form><legend id='xoair'></legend><bdo id='xoair'><pre id='xoair'><center id='xoair'></center></pre></bdo></b><th id='xoair'></th></span></q></dt></tr></i><div class="7f55bhj" id='xoair'><tfoot id='xoair'></tfoot><dl id='xoair'><fieldset id='xoair'></fieldset></dl></div>

        <tfoot id='xoair'></tfoot>

            <tbody id='xoair'></tbody>

              • <legend id='xoair'><style id='xoair'><dir id='xoair'><q id='xoair'></q></dir></style></legend>
                本文介紹了python:不同包下同名的兩個模塊和類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我已經開始學習python并編寫一個練習應用程序.目錄結構看起來像

                I have started to learn python and writing a practice app. The directory structure looks like

                src
                 |
                 --ShutterDeck
                    |
                    --Helper
                       |
                       --User.py -> class User
                    --Controller
                       |
                       --User.py -> class User
                

                src 目錄位于 PYTHONPATH 中.在另一個文件中,比如說 main.py,我想訪問兩個 User 類.我該怎么做.

                The src directory is in PYTHONPATH. In a different file, lets say main.py, I want to access both User classes. How can I do it.

                我嘗試使用以下方法,但失敗了:

                I tried using the following but it fails:

                import cherrypy
                from ShutterDeck.Controller import User
                from ShutterDeck.Helper import User
                
                class Root:
                  @cherrypy.expose
                  def index(self):
                    return 'Hello World'
                
                u1=User.User()
                u2=User.User()
                

                這肯定是模棱兩可的.我能想到的另一種(c++ 實現方式)方式是

                That's certainly ambiguous. The other (c++ way of doing it) way that I can think of is

                import cherrypy
                from ShutterDeck import Controller
                from ShutterDeck import Helper
                
                class Root:
                
                  @cherrypy.expose
                  def index(self):
                    return 'Hello World'
                
                u1=Controller.User.User()
                u2=Helper.User.User()
                

                但是當上面的腳本運行時,它給出了以下錯誤

                But when above script is run, it gives the following error

                u1=Controller.User.User()
                AttributeError: 'module' object has no attribute 'User'
                

                我無法弄清楚為什么會出錯?ShutterDeckHelperController 目錄中有 __init__.py.

                I'm not able to figure out why is it erroring out? The directories ShutterDeck, Helper and Controller have __init__.py in them.

                推薦答案

                您要導入包 __init__.py 文件中的 User 模塊,使其可用作屬性.

                You want to import the User modules in the package __init__.py files to make them available as attributes.

                所以在 Helper/__init_.pyController/__init__.py 中添加:

                So in both Helper/__init_.py and Controller/__init__.py add:

                from . import User
                

                這使模塊成為包的屬性,您現在可以這樣引用它.

                This makes the module an attribute of the package and you can now refer to it as such.

                或者,您必須自己完整導入模塊:

                Alternatively, you'd have to import the modules themselves in full:

                import ShutterDeck.Controller.User
                import ShutterDeck.Helper.User
                
                u1=ShutterDeck.Controller.User.User()
                u2=ShutterDeck.Helper.User.User()
                

                所以用他們的全名來稱呼他們.

                so refer to them with their full names.

                另一種選擇是使用 as 重命名導入的名稱:

                Another option is to rename the imported name with as:

                from ShutterDeck.Controller import User as ControllerUser
                from ShutterDeck.Helper import User as HelperUser
                
                u1 = ControllerUser.User()
                u2 = HelperUser.User()
                

                這篇關于python:不同包下同名的兩個模塊和類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Configuring Python to use additional locations for site-packages(配置 Python 以使用站點包的其他位置)
                How to structure python packages without repeating top level name for import(如何在不重復導入頂級名稱的情況下構造python包)
                Install python packages on OpenShift(在 OpenShift 上安裝 python 包)
                How to refresh sys.path?(如何刷新 sys.path?)
                Distribute a Python package with a compiled dynamic shared library(分發帶有已編譯動態共享庫的 Python 包)
                R, Python: install packages on rpy2(R,Python:在 rpy2 上安裝包)
                      <bdo id='1encF'></bdo><ul id='1encF'></ul>
                      <i id='1encF'><tr id='1encF'><dt id='1encF'><q id='1encF'><span id='1encF'><b id='1encF'><form id='1encF'><ins id='1encF'></ins><ul id='1encF'></ul><sub id='1encF'></sub></form><legend id='1encF'></legend><bdo id='1encF'><pre id='1encF'><center id='1encF'></center></pre></bdo></b><th id='1encF'></th></span></q></dt></tr></i><div class="rfd7zfl" id='1encF'><tfoot id='1encF'></tfoot><dl id='1encF'><fieldset id='1encF'></fieldset></dl></div>
                    • <legend id='1encF'><style id='1encF'><dir id='1encF'><q id='1encF'></q></dir></style></legend>

                      1. <tfoot id='1encF'></tfoot>

                            <tbody id='1encF'></tbody>

                          <small id='1encF'></small><noframes id='1encF'>

                          主站蜘蛛池模板: 老城街小面官网_正宗重庆小面加盟技术培训_特色面馆加盟|牛肉拉面|招商加盟代理费用多少钱 | 稳尚教育加盟-打造高考志愿填报平台_新高考志愿填报加盟_学业生涯规划加盟 | 西装定制/做厂家/公司_西装订做/制价格/费用-北京圣达信西装 | 众品家具网-家具品牌招商_家具代理加盟_家具门户的首选网络媒体。 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | 工业用品一站式采购平台|南创工品汇-官网|广州南创 | 衬氟止回阀_衬氟闸阀_衬氟三通球阀_衬四氟阀门_衬氟阀门厂-浙江利尔多阀门有限公司 | 上海璟文空运首页_一级航空货运代理公司_机场快递当日达 | 识禅_对禅的了解,从这里开始| 北京开源多邦科技发展有限公司官网| 双相钢_双相不锈钢_双相钢圆钢棒_双相不锈钢报价「海新双相钢」 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 深圳善跑体育产业集团有限公司_塑胶跑道_人造草坪_运动木地板 | 食药成分检测_调料配方还原_洗涤剂化学成分分析_饲料_百检信息科技有限公司 | 龙门加工中心-数控龙门加工中心厂家价格-山东海特数控机床有限公司_龙门加工中心-数控龙门加工中心厂家价格-山东海特数控机床有限公司 | 双杰天平-国产双杰电子天平-美国双杰-常熟双杰仪器 | 电动不锈钢套筒阀-球面偏置气动钟阀-三通换向阀止回阀-永嘉鸿宇阀门有限公司 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | 安平县鑫川金属丝网制品有限公司,声屏障,高速声屏障,百叶孔声屏障,大弧形声屏障,凹凸穿孔声屏障,铁路声屏障,顶部弧形声屏障,玻璃钢吸音板 | 珠海网站建设_响应网站建设_珠海建站公司_珠海网站设计与制作_珠海网讯互联 | 济南网站建设|济南建网站|济南网站建设公司【济南腾飞网络】【荐】 | 中药超微粉碎机(中药细胞级微粉碎)-百科 | 云杂志网-学术期刊-首页 | RS系列电阻器,RK_RJ启动调整电阻器,RQ_RZ电阻器-上海永上电器有限公司 | SMC-ASCO-CKD气缸-FESTO-MAC电磁阀-上海天筹自动化设备官网 | 面粉仓_储酒罐_不锈钢储酒罐厂家-泰安鑫佳机械制造有限公司 | 【铜排折弯机,钢丝折弯成型机,汽车发泡钢丝折弯机,线材折弯机厂家,线材成型机,铁线折弯机】贝朗折弯机厂家_东莞市贝朗自动化设备有限公司 | 自动化改造_智虎机器人_灌装机_贴标机-上海圣起包装机械 | 一点车讯-汽车网站,每天一点最新车讯! | 新型游乐设备,360大摆锤游乐设备「诚信厂家」-山东方鑫游乐设备 新能源汽车电池软连接,铜铝复合膜柔性连接,电力母排-容发智能科技(无锡)有限公司 | 昆山新莱洁净应用材料股份有限公司-卫生级蝶阀,无菌取样阀,不锈钢隔膜阀,换向阀,离心泵 | 桁架楼承板-钢筋桁架楼承板-江苏众力达钢筋楼承板厂 | EPDM密封胶条-EPDM密封垫片-EPDM生产厂家 | 西安中国国际旅行社(西安国旅) | 防渗膜厂家|养殖防渗膜|水产养殖防渗膜-泰安佳路通工程材料有限公司 | 滑石粉,滑石粉厂家,超细滑石粉-莱州圣凯滑石有限公司 | 工业CT-无锡璟能智能仪器有限公司 | 合肥白癜风医院_[治疗白癜风]哪家好_合肥北大白癜风医院 | MVE振动电机_MVE震动电机_MVE卧式振打电机-河南新乡德诚生产厂家 | 上海皓越真空设备有限公司官网-真空炉-真空热压烧结炉-sps放电等离子烧结炉 | 无缝方管|无缝矩形管|无缝方矩管|无锡方管厂家 | 洛阳网站建设_洛阳网站优化_网站建设平台_洛阳香河网络科技有限公司 |