??? Codes
? ??? GeneralizedInversion.py
? ??? GeometricalSpreading.py
? ??? HtoV.py
? ??? __init__.py
? ??? interface
? ? ??? __init__.py
? ? ??? pyqtinter.py
? ??? rocksiteamplification.py
? ??? TheSoftware.py
Run Code Online (Sandbox Code Playgroud)
在pyqtinter.py 中,我尝试使用导入TheSoftware.py
from .. import TheSoftware
Run Code Online (Sandbox Code Playgroud)
但错误是:ValueError: Attempted relative import in non-package
此链接清除了出现此错误的原因: 第 10 亿次相对进口
为了解决这个问题,两个主要的解决方案是在执行 python 代码时使用 sys.path.append(path) 或使用 -m 所以没有__name__ 设置为__main__。据说可选属性__package__也可用于解决此问题,但我还没有找到方法。问题是如果我设置__package__为 Codes.interface那么 python 有可能理解相对导入吗?
from .. import模块正常工作,模块名称的点数必须至少与语句中的点数一样多import。__main__我喜欢在编写生产代码之前使用交互式 Jupyter Notebook 会话来探索数据和测试模块。为了让事情变得清晰易懂,让队友可以理解,我喜欢将笔记本放在一个interactive包中,与我正在测试的包和模块并排放置。
package/
__init__.py
subpackage1/
__init__.py
moduleX.py
moduleY.py
moduleZ.py
subpackage2/
__init__.py
moduleZ.py
interactive/
__init__.py
my_notebook.ipynb
Run Code Online (Sandbox Code Playgroud)
在交互式会话期间interactive.my_notebook.ipynb,您将如何导入其他模块,例如subpackage1.moduleX和subpackage2.moduleZ?
我的结构字典是
mainFolder
folder1
__init__.py
file1.py
file2.py
folder2
__init__.py
file3.py
file4.py
setup.py
__init__.py
Run Code Online (Sandbox Code Playgroud)
我需要将 file4.py 从 folder2 导入到 folder1/file1.py
file1.py:
从 ..folder2.file4 导入 MyClass
我得到:
SystemError: 父模块 '' 未加载,无法执行相对导入
如何解决?
我需要在我的 python 项目中执行一个函数的导入。
我知道关于 SO 有很多类似的问题,但是,不幸的是,我找不到适合我的解决方案,因为答案要么过于具体,要么过于笼统,或者它们只是丑陋的黑客(例如操作使用绝对路径)。
这是我的文件夹结构的样子:
PythonClient:.
? .gitignore
? des.py
? des_test.py
? des_var2.py
? gui.py
? index.py
? __init__.py
?
????diffie_hellman
? ? diffie_hellman.py
? ? diffie_hellman_test.py
? ? __init__.py
? ?
? ????__pycache__
? diffie_hellman.cpython-35.pyc
?
????hashes
? ? collision.py
? ? hash_function.py
? ? __init__.py
? ?
? ????__pycache__
? hash_function.cpython-35.pyc
? __init__.cpython-35.pyc
?
????__pycache__
des.cpython-35.pyc
des_var2.cpython-35.pyc
Run Code Online (Sandbox Code Playgroud)
我需要./hashes/hash_function.py从./diffie_hellman/diffie_hellman.py.
该./hashes/hash_function.py文件包含唯一名为hash_function.
我已经尝试了很多方法来执行导入,但就是做不到。我总是得到
SystemError: 父模块 '' 未加载,无法执行相对导入
当我.在导入语句中使用时(即 …
如果设置language_level=2为ext_modules = cythonize(extensions, language_level=2),那会发生什么变化?只是我编写的代码应该解释为Python2吗?
最终结果完全一样吗?
这是我使用 Django 的第一个项目,也是我第一次使用 Docker。到目前为止,我真的很喜欢它们,但我面临一个一周后无法解决的问题。
\n我按照本教程构建我的项目:https://medium.com/swlh/setting-up-a-secure-django-project-repository-with-docker-and-django-environ-4af72ce037f0
\n到目前为止,我的项目结构如下所示:
\nMyProject\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 backend # Django "app"\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 admin.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 apps.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 models.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 permissions.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 serializers.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tests.py\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 views.py \n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config # Django "project"\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 asgi.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 settings.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 urls.py\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 wsgi.py \n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 docker # Docker configuration files, content close to the source link above\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ... \n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 frontend # independent angular project\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ... \n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py <====== # The file responsible of all the trouble …Run Code Online (Sandbox Code Playgroud) 我有一个 Python 项目的文件夹结构,如下所示:
proj/
??? cars
? ??? honda.py
? ??? volvo.py
??? trucks
? ??? chevy.py
? ??? ford.py
??? main.py
??? params.py
Run Code Online (Sandbox Code Playgroud)
内容params.py:
""" Parameters used by other files. """
serial = '12-411-7843'
Run Code Online (Sandbox Code Playgroud)
内容honda.py:
""" Information about Honda car. """
from params import serial
year = 1988
s = serial
print('year is', year)
print('serial is', s)
Run Code Online (Sandbox Code Playgroud)
在proj/文件夹中,我可以使用 iPython 运行脚本:
$ cd path/to/proj/
$ ipython
In [1]: run cars/honda.py
year is 1988
serial …Run Code Online (Sandbox Code Playgroud) 我的项目有以下结构
Object_Detection/
setup.py
setup.cfg
requirement.txt
object_detection/
models
__init__.py #contains from . import models
tests/
# inside tests dir
test_utils_image.py
__init__.py #empty
utils/
# inside utils dir
__init__.py #inside
utils_image_preprocess.py
utils_image.py
utils_tfrecord.py
Run Code Online (Sandbox Code Playgroud)
现在utils 目录中的init .py 包含以下代码。
# inside __init__.py
from . import utils_image_preprocess
from . import utils_image
from . import utils_tfrecord
Run Code Online (Sandbox Code Playgroud)
在init .py 文件之上运行会出现错误:
ImportError: attempted relative import with no known parent package
Run Code Online (Sandbox Code Playgroud)
测试目录中的 test_utils.py 包含以下代码
# inside test_utils.py
from object_detection.utils import utils_image
Run Code Online (Sandbox Code Playgroud)
运行 test_utils.py 时出现以下错误
ImportError: cannot import name …Run Code Online (Sandbox Code Playgroud) 我正在阅读Python教程,然后我进入了关于模块的部分.
我在Users/Me/code/Python中创建了一个fibo.py文件
现在我回到解释器中,我似乎无法导入模块,因为我不明白如何导入相对(或绝对)路径.
我也对如何以及是否修改PYTHONPATH和/或sys.path感到困惑.
所有其他'导入模块'问题似乎都在这里
我正在寻找一种从另一个文件夹导入函数的简单方法:
|--mylib
| |--__init__.py
| |--mylib2.py
|
|--mytest
| |--other.py
| |--test_one.py
|
|--mymain.py
Run Code Online (Sandbox Code Playgroud)
mylib2.py
def suma(a,b):
return a+b
Run Code Online (Sandbox Code Playgroud)
mymain.py
from mylib.mylib2 import suma
def main():
a=3
b=4
c=suma(a,b)
print(c)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
其他.py
from mylib.mylib2 import suma
c=suma(1,2)
print(c)
Run Code Online (Sandbox Code Playgroud)
测试一.py
from mylib.mylib2 import suma
def test_nothing():
assert True
def test_crash():
e=0
#d=30/e
assert True
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我无法正常从 other.py 调用 suma (因为我不知道如何正确地将 mylib2.py 包含在 mytest 文件夹中的文件中,但按照这个答案我可以这样做
python3 -m mytest.other
3
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何运行 pytest(包括库)
如果我进行 pytest 我会收到一条消息
from mylib.mylib2 import …Run Code Online (Sandbox Code Playgroud) python ×9
python-3.x ×3
ipython ×2
package ×2
cython ×1
django ×1
docker ×1
import ×1
module ×1
oop ×1
pytest ×1
python-2.7 ×1
testing ×1
unit-testing ×1