仅在3.x上使用Python包的ImportError
我在PyPA上有一个名为Airship的Python包,它在仅在3.x上使用Python包的ImportError,python,pip,python-3.4,Python,Pip,Python 3.4,我在PyPA上有一个名为Airship的Python包,它在\uuu init\uuu.py中有以下代码: import os from airship import sync class suppress_stdout_stderr(object): # http://stackoverflow.com/questions/11130156 def __init__(self): self.null_fds = [os.open(os.devnull,os.O_RDW
\uuu init\uuu.py
中有以下代码:
import os
from airship import sync
class suppress_stdout_stderr(object): # http://stackoverflow.com/questions/11130156
def __init__(self):
self.null_fds = [os.open(os.devnull,os.O_RDWR) for x in range(2)]
self.save_fds = (os.dup(1), os.dup(2))
def __enter__(self):
os.dup2(self.null_fds[0], 1)
os.dup2(self.null_fds[1], 2)
def __exit__(self, *_):
os.dup2(self.save_fds[0], 1)
os.dup2(self.save_fds[1], 2)
os.close(self.null_fds[0])
os.close(self.null_fds[1])
def main():
with suppress_stdout_stderr():
sync()
sync()
是在\uuuu main\uuuuuuuuuuuupy
中定义的,我在setup.py
中有以下几行代码将入口点定义为脚本:
entry_points={
'console_scripts': [
'airship=airship:main'
]
}
当我在Python2.7.9安装中从pip
安装飞艇后运行airship
时,它会按预期运行(也就是说,没有输出)
在Python 3.4.3安装上安装了来自pip
的相同软件包后,我尝试执行相同的操作时,会出现以下错误:
Traceback (most recent call last):
File "C:\Python34\lib\runpy.py", line 170, in _run_module_as_main
"__main__", mod_spec)
File "C:\Python34\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Python34\Scripts\airship.exe\__main__.py", line 5, in <module>
File "C:\Python34\lib\site-packages\airship\__init__.py", line 2, in <module>
from airship import sync
ImportError: cannot import name 'sync'
回溯(最近一次呼叫最后一次):
文件“C:\Python34\lib\runpy.py”,第170行,在运行模块中作为主
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
文件“C:\Python34\lib\runpy.py”,第85行,在运行代码中
exec(代码、运行\全局)
文件“C:\Python34\Scripts\airship.exe\ \uuuuu main\uuuuuuu.py”,第5行,在
文件“C:\Python34\lib\site packages\airship\\uuuuu init\uuuuuuu.py”,第2行,在
从飞艇导入同步
ImportError:无法导入名称“sync”
这是什么原因造成的?我如何修复它
(注意:我只试过在Python 3.x版的pip
上运行airship
)
Windows,这是我遇到此问题的地方。我已成功地在OS x和Debian上的Python 2.x安装上运行了
airship
。在Python 3中,您需要使用显式相对导入,因此要在项目中导入顶级sync
模块:
from . import sync
如果sync
是飞艇的子模块,请执行以下操作:
from .airship import sync
相对导入样式是用添加到python 2.4、2.5和2.6中的,但直到python 3才删除隐式相对导入。我不认为这只是python 3的事情,正如我在2.6.5中也需要这样做一样,根据python 2.4支持的相对导入,DownVoting因为这个答案混淆了绝对和相对。@user2357112请随意编辑以澄清您认为需要它的地方。sync()
是一个函数,而不是一个模块。这不能回答我的问题。但是,是否可以将\uuuuu main\uuuuuuuuuuupy
作为模块从\uuuuu init\uuuuuuuuuuuuuuuuuuupy
导入,然后调用module.sync()
?PyPI(因为它少于6个字符,我无法编辑它)。