2

你好 stackoverflow 社区再次。我还有一个问题。

最近,我发现了一个 Python 库,它对我最近的一个名为 Ghost.py 的项目非常有用。这个库是一个网络库。

我遇到的问题是一个相当奇怪的错误。谷歌没有出现任何相关信息。

from .ghost import Ghost
SystemError: Parent module '' not loaded, cannot perform relative import

如您所见,这是一个相当奇怪的错误。感谢您对解决此错误的任何帮助,谢谢。

4

3 回答 3

2

你有一个额外.的在鬼面前。我认为你不需要它:

from ghost import Ghost
于 2013-07-15T02:26:00.447 回答
2

为Babby Boss +1,因为他/她的解决方案会起作用,但是我很懒惰,当我也遇到问题时我来到这里,我的问题是通过我卸载解决的Ghost,我使用pip像OP一样安装它而不是安装Ghost.py

注意GhostGhost.py不一样,Ghost如果像我一样,使用 pip 安装会导致错误Ghost.py

如此简单的修复=
pip2 uninstall Ghost(等待此完成)
pip2 install Ghost.py

于 2017-09-07T20:06:08.293 回答
0

Apart from writing correct code like from ghost import Ghost, you may like to follow steps outlined below.

Download ghost.py master file from https://github.com/jeanphix/Ghost.py/archive/master.zip

Unzip contents of master.zip to C:\ghost-master folder

If you carefully watch the folder structure then you find that ghost\__init__.py file has following content.

from .ghost import Ghost, Error, TimeoutError
from .test import GhostTestCase

While ghost\ext\__init__.py is of 0 KB

In this case, when you run following command to build the package.

C:\<path_to_Python_folder>\python.exe setup.py build

you see that apart from other lines, following line also appears.

copying ghost\ext\__init__.py -> build\lib\ghost\ext

It means that __init__.py of 0 KB size is being copied as build\lib\ghost\ext\__init__.py

Therefore, even though installing ghost.py with following command does not produce any error, you face ImportError: cannot import name Ghost.

C:\<path_to_Python_folder>\python.exe setup.py install

So, to solve the problem, before building the package overwrite __init__.py file

C:\ghost-master>copy ghost\__init__.py ghost\ext\   

Now issue following commands.

C:\<path_to_Python_folder>\python.exe setup.py build
C:\<path_to_Python_folder>\python.exe setup.py install

Now you will not see ImportError: cannot import name Ghost

I have tested above solution with Python 2.7.6

于 2014-06-30T09:20:02.483 回答