久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

    1. <legend id='lLgsz'><style id='lLgsz'><dir id='lLgsz'><q id='lLgsz'></q></dir></style></legend>

      • <bdo id='lLgsz'></bdo><ul id='lLgsz'></ul>

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

        兄弟包導入

        Sibling package imports(兄弟包導入)
        • <bdo id='aRss7'></bdo><ul id='aRss7'></ul>
          <i id='aRss7'><tr id='aRss7'><dt id='aRss7'><q id='aRss7'><span id='aRss7'><b id='aRss7'><form id='aRss7'><ins id='aRss7'></ins><ul id='aRss7'></ul><sub id='aRss7'></sub></form><legend id='aRss7'></legend><bdo id='aRss7'><pre id='aRss7'><center id='aRss7'></center></pre></bdo></b><th id='aRss7'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='aRss7'><tfoot id='aRss7'></tfoot><dl id='aRss7'><fieldset id='aRss7'></fieldset></dl></div>

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

            • <tfoot id='aRss7'></tfoot>

                <legend id='aRss7'><style id='aRss7'><dir id='aRss7'><q id='aRss7'></q></dir></style></legend>
                  <tbody id='aRss7'></tbody>

                  本文介紹了兄弟包導入的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我嘗試閱讀有關同級導入的問題,甚至包文檔,但我還沒有找到答案.

                  I've tried reading through questions about sibling imports and even the package documentation, but I've yet to find an answer.

                  結構如下:

                  ├── LICENSE.md
                  ├── README.md
                  ├── api
                  │?? ├── __init__.py
                  │?? ├── api.py
                  │?? └── api_key.py
                  ├── examples
                  │?? ├── __init__.py
                  │?? ├── example_one.py
                  │?? └── example_two.py
                  └── tests
                  │?? ├── __init__.py
                  │?? └── test_one.py
                  

                  examplestests 目錄下的腳本如何從api 模塊并從命令行運行?

                  How can the scripts in the examples and tests directories import from the api module and be run from the commandline?

                  另外,我想避免對每個文件進行丑陋的 sys.path.insert hack.一定這可以在 Python 中完成,對吧?

                  Also, I'd like to avoid the ugly sys.path.insert hack for every file. Surely this can be done in Python, right?

                  推薦答案

                  七年后

                  自從我在下面寫了答案后,修改 sys.path 仍然是一個快速而骯臟的技巧,適用于私有腳本,但已經有了一些改進

                  Seven years after

                  Since I wrote the answer below, modifying sys.path is still a quick-and-dirty trick that works well for private scripts, but there has been several improvements

                    盡管我建議使用 pip這樣做而不是直接使用 setuptools(并使用 setup.cfg 來存儲元數據)
                  • 使用 -m 標志并作為包運行也可以(但會轉如果你想把你的工作目錄轉換成一個可安裝的包,那就有點尷尬了).
                  • 對于測試,具體來說,pytest 能夠在這種情況下找到 api 包并處理 sys.path 為您提供的技巧
                  • Installing the package (in a virtualenv or not) will give you what you want, though I would suggest using pip to do it rather than using setuptools directly (and using setup.cfg to store the metadata)
                  • Using the -m flag and running as a package works too (but will turn out a bit awkward if you want to convert your working directory into an installable package).
                  • For the tests, specifically, pytest is able to find the api package in this situation and takes care of the sys.path hacks for you

                  所以這真的取決于你想做什么.但是,在您的情況下,由于您的目標似乎是在某個時候制作適當的軟件包,因此通過 pip -e 安裝可能是您最好的選擇,即使它還不完美.

                  So it really depends on what you want to do. In your case, though, since it seems that your goal is to make a proper package at some point, installing through pip -e is probably your best bet, even if it is not perfect yet.

                  正如在其他地方已經說過的,可怕的事實是,你必須做一些丑陋的 hack 才能允許從 __main__ 模塊導入兄弟模塊或父包.PEP 366 中詳細介紹了該問題.PEP 3122 試圖以更合理的方式處理導入,但 Guido 拒絕了它一個帳號

                  As already stated elsewhere, the awful truth is that you have to do ugly hacks to allow imports from siblings modules or parents package from a __main__ module. The issue is detailed in PEP 366. PEP 3122 attempted to handle imports in a more rational way but Guido has rejected it one the account of

                  唯一的用例似乎是運行發生的腳本生活在一個模塊的目錄中,我一直將其視為一個反模式.

                  The only use case seems to be running scripts that happen to be living inside a module's directory, which I've always seen as an antipattern.

                  (這里)

                  不過,我經常使用這種模式

                  Though, I use this pattern on a regular basis with

                  # Ugly hack to allow absolute import from the root folder
                  # whatever its name is. Please forgive the heresy.
                  if __name__ == "__main__" and __package__ is None:
                      from sys import path
                      from os.path import dirname as dir
                  
                      path.append(dir(path[0]))
                      __package__ = "examples"
                  
                  import api
                  

                  這里 path[0] 是您正在運行的腳本的父文件夾,而 dir(path[0]) 是您的頂級文件夾.

                  Here path[0] is your running script's parent folder and dir(path[0]) your top level folder.

                  盡管如此,我仍然無法使用相對導入,但它確實允許從頂層(在您的示例 api 的父文件夾中)進行絕對導入.

                  I have still not been able to use relative imports with this, though, but it does allow absolute imports from the top level (in your example api's parent folder).

                  這篇關于兄弟包導入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  python: Two modules and classes with the same name under different packages(python:不同包下同名的兩個模塊和類)
                  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 包)
                    <legend id='Qu33H'><style id='Qu33H'><dir id='Qu33H'><q id='Qu33H'></q></dir></style></legend>
                    <i id='Qu33H'><tr id='Qu33H'><dt id='Qu33H'><q id='Qu33H'><span id='Qu33H'><b id='Qu33H'><form id='Qu33H'><ins id='Qu33H'></ins><ul id='Qu33H'></ul><sub id='Qu33H'></sub></form><legend id='Qu33H'></legend><bdo id='Qu33H'><pre id='Qu33H'><center id='Qu33H'></center></pre></bdo></b><th id='Qu33H'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Qu33H'><tfoot id='Qu33H'></tfoot><dl id='Qu33H'><fieldset id='Qu33H'></fieldset></dl></div>
                      <tbody id='Qu33H'></tbody>
                  • <small id='Qu33H'></small><noframes id='Qu33H'>

                  • <tfoot id='Qu33H'></tfoot>

                          • <bdo id='Qu33H'></bdo><ul id='Qu33H'></ul>

                            主站蜘蛛池模板: 午夜在线| 亚洲美女在线视频 | 在线观看成人精品 | 欧美激情免费在线 | 久久精品小视频 | 色视频在线观看 | 武道仙尊动漫在线观看 | 色婷婷综合网 | 在线毛片网 | 国产成人精品免费 | 欧美13videosex性极品 | 91亚洲欧美 | 91视频一区二区三区 | 人碰人操 | 久久久久成人精品 | 国产日韩欧美激情 | 精品免费国产视频 | 亚洲自拍偷拍免费视频 | 国产美女精品视频 | 久久99久久 | 97精品视频在线观看 | 成人亚洲片 | 亚洲久久一区 | 国产日韩av一区二区 | 日韩不卡在线观看 | 免费一级欧美在线观看视频 | 91视频网址 | 亚洲精品久久久久久一区二区 | 亚洲精品日韩在线 | 日韩在线一区二区 | 成人免费在线 | 在线观看黄色大片 | 亚洲国产一区视频 | 日韩在线免费看 | 日韩欧美精品一区 | 成人黄页在线观看 | 亚洲欧美第一视频 | 日日碰狠狠躁久久躁婷婷 | 亚洲色图网址 | 欧美在线视频一区二区 | 精品久久久久久一区二区 |