0

我将 Python 3 和 SOAPpy 库与我的代码一起使用。ModuleNotFoundError: No module named 'version'尝试运行我的代码时,我不断收到“ ”错误。错误来自库中的这一特定行:

from version import __version__

如何修复错误?

4

2 回答 2

1

People usually set the variable __version__ in a module to make it available to the world to inspect, it even has a PEP!

I'm not sure about which line you are referring to but there are many with the same incriminating import in the SOAPpy package.

You can see here that the package indeed has a version module defining a __version__ variable. I don't know how are you including this package in your project but I'll explain something about imports, you can read more here.

The incriminating line can have two alternatives

from .version import __version__ # relative import

from SOAPpy.version import __version__ # absolute import

You could read them as "paths", the first is the version.py file in the current directory, the second is the version.py file in the SOAPpy directory.

They are both fine but sometimes repeating SOAPpy everywhere is too much work.

Let's fix the line to have a relative import

diff --git a/SOAPpy/__init__.py b/SOAPpy/__init__.py
index 0e039f8..832a560 100644
--- a/SOAPpy/__init__.py
+++ b/SOAPpy/__init__.py
@@ -1,15 +1,15 @@

 ident = '$Id: __init__.py,v 1.9 2004/01/31 04:20:06 warnes Exp $'
-from version import __version__
+from .version import __version__

-from Client      import *
-from Config      import *
-from Errors      import *
-from NS          import *
-from Parser      import *
-from SOAPBuilder import *
-from Server      import *
-from Types       import *
-from Utilities     import *
+from .Client      import *
+from .Config      import *
+from .Errors      import *
+from .NS          import *
+from .Parser      import *
+from .SOAPBuilder import *
+from .Server      import *
+from .Types       import *
+from .Utilities     import *
 import wstools
 import WSDL

And indeed now if I run python setup.py install I get a different error

Traceback (most recent call last):
  File "setup.py", line 8, in <module>
    from SOAPpy.version import __version__
  File "/home/edoput/repo/SOAPpy/SOAPpy/__init__.py", line 5, in <module>
    from .Client      import *
  File "/home/edoput/repo/SOAPpy/SOAPpy/Client.py", line 95
    raise IOError, "unsupported SOAP protocol"
                 ^
SyntaxError: invalid syntax

So indeed it might be a problem that this library has not been upgraded to use python3!

Let's use python2 instead, even if we should not.

virtualenv env --python=python2
source env/bin/activate
python setup.py install

And now everything installs correctly, this library has definitely not been updated recently, you can still use it but you have to stick to use python 2.7. Have fun!

于 2020-01-07T19:11:55.487 回答
0

SOAPpy ( pypy ) 似乎是一个用于 python2 的过时库,这就是库中的某些代码产生这些错误的原因。使用Zeep - 一个现代 SOAP python3 兼容客户端可能会更好。

于 2020-01-07T18:52:26.960 回答