English 中文(简体)
相对进口的粉饰包装
原标题:python packaging for relative imports

首先: 我很抱歉,我知道有关相对进口的问题很多,但我只是找不到解决办法。 如果可能的话,我想使用以下目录:

myClass/
    __init__.py
    test/
        demo.py
        benchmark.py
        specs.py
    src/
        __init__.py
        myClass.py

现在我的问题是:

  • 一揽子计划中的测试档案如何适当进口我的地图集?

  • 假设你在校准/米地图册中把我的地图集作为分层,或列入/记录,你将如何从外部进口这套材料?

至今,我无法找到这方面的一个棘手解决办法。 http://www.python.org/dev/peps/pep-0328/#guido-s-decision”rel=“noreferer” 可在<代码>上填写。

<代码> 错误: 企图在非包装材料/编码中相对进口

http://docs.python.org/tutorial/modules.html# Packages”rel=“noreferer” 阅读:

__init__.py档案必须作为包件处理。

我似乎缺少一些具体内容,说明一揽子计划的内容是什么?

问题回答

ValueError: Attempted relative import in non-package

您试图在并非包裹的模块中使用相对进口。 其问题在于从......进口<>/代码>到你试图进口的文档。

因此,如果你在测试中进行相对进口,例如,你应当把测试作为你的一揽子计划的一部分。 这意味着:

  1. Adding __init__.py to test/
  2. Running them from some outside script, like nosetests

If you run something as python myClass/test/demo.py, relative imports will not work too since you are running demo module not as package. Relative imports require that the module which uses them is being imported itself either as package module, from myClass.test.demo import blabla, or with relative import.

昨天晚上,在搜查几个小时之后,我找到了在猪口相对进口的答案! 或至少是容易的解决办法。 解决这一问题的最佳途径是从另一个模块中调取单元。 因此,请您在进口<条码>demo.py时使用。 在<代码>myClass中,夹在子包装的根基上,他们需要一个叫做另外两个的文件。 从我所收集的工作名录一直被视为__main__。 如果你用<代码>demo.py对进口产品进行测试,则你会收到这一错误。 说明:

等级:

myClass/
    main.py #arbitrary name, can be anything
    test/
        __init__.py
        demo.py
    src/
        __init__.py
        myClass.py

我的地图集:

def randomMaths(x):
    a = x * 2
    y = x * a
    return y

demo.py:

from ..src import myClass

def printer():
    print(myClass.randomMaths(42))

主编:

import test.demo

demo.printer()

如果你在口译中操作<代码>demo.py,则你将产生错误,但<代码>main.py将不适用。 它是一纸空文。

Intra-package-references describes how to myClass from test/*. To import the package from outside, you should add its path to PYTHONPATH environment variable before running the importer application, or to sys.path list in the code before importing it.

为什么从.......src进口我的Class失败:可能的话,src 这不是一件on子,你不能从那里进口。 你们应该如上所述,把它补充到 path路上。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签