简体   繁体   English

从相邻模块导入软件包在Py2中有效,但在Py3中无效

[英]Import package from adjacent module works in Py2 but not in Py3

I'm still getting used to structuring python projects and relative imports, I thought I understood relative imports mostly until I ran into an issue when testing on Py3. 我仍然习惯于构造python项目和相对导入,我以为我最了解相对导入,直到在Py3上进行测试时遇到一个问题。

I have a project that is structure like so: 我有一个结构如下的项目:

scriptA.py
package/__init__.py
          scriptB.py
          scriptC.py

and __init.py__ contains the following: __init.py__包含以下内容:

from scriptB import functionB
from scriptC import functionC

In scriptA import package as _package works in Py2.7, but fails on Py3.5 with the error, ImportError: No module named 'scriptB' . scriptA import package as _packagescriptA中可用,但在Py3.5上失败,并显示错误ImportError: No module named 'scriptB'

How can I import package in a way that is compatible with both Py2 and 3? 如何以与Py2和3兼容的方式导入package Why is this different? 为什么有什么不同?

I tried doing import .package as _package but that doesn't seem to change anything (still figuring out when to use . and .. ... 我尝试import .package as _package但这似乎并没有改变任何内容(仍在确定何时使用... ...

No module named 'scriptB' 没有名为“ scriptB”的模块

Looks like a path error. 看起来像路径错误。 Check your path statement. 检查您的路径语句。 The path statement in Windows can be accessed by typing "system" into start menu search bar. 通过在开始菜单搜索栏中键入“ system”,可以访问Windows中的path语句。 From there you can access "Environment Variables", one of which is the "path" statement. 从那里您可以访问“环境变量”,其中之一是“路径”语句。 There you can see different directories that are allowed to be accessed by Python scripts. 在这里,您可以看到Python脚本允许访问的不同目录。 Make sure your Python 3.x directory and Python 3.x/scripts directory are listed there. 确保在那里列出了Python 3.x目录和Python 3.x / scripts目录。
Files listed in the path statement are kind of like Global Variables in a program. path语句中列出的文件有点像程序中的全局变量。 If both Python 2 and Python 3 have the same file names in their directories you could access an incompatible file. 如果Python 2和Python 3在其目录中具有相同的文件名,则可以访问不兼容的文件。 I've read that there is a python launcher which will solve this very problem so that you can run multiple versions of python on the same computer. 我读到有一个python启动器可以解决这个问题,因此您可以在同一台计算机上运行多个版本的python。 For now I would test to see if this is your problem by -copying the path statement to a backup file and then -remove references to python 2.x, -reboot -run your program. 现在,我将通过-将path语句复制到备份文件,然后-删除对python 2.x的引用,-reboot-运行程序来测试是否是您的问题。 If you want to run multiple versions of python copy the original path statement back and install the python launcher. 如果要运行多个版本的python,请复制原始path语句,然后安装python启动器。

So the problem was that the imports in __init__.py should have been relative imports ie: 因此,问题在于__init__.py中的导入应该是相对导入,即:

from .scriptB import functionB
from .scriptC import functionC

I guess this is one of the differences between importing a module and a python package. 我想这是导入模块和python包之间的区别之一。 It seems that in Py3.5 relative imports need to be done explicitly and so an error was thrown. 似乎在Py3.5中,需要显式完成相对导入,因此引发了错误。 This is my interpretation. 这是我的解释。 unfortunatly the structure I had put in the question was not close enough to the problem I had, so this doesn't solve it... But a more detailed (and probably accurate) answer for this is still welcomed. 不幸的是,我在问题中提出的结构与我所遇到的问题不够接近,因此无法解决问题……但是,仍欢迎对此进行更详细的(可能是准确的)答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM