2014-11-04 145 views
1

當我輸入:help('modules')時,我無法導入這個模塊,不知道是什麼問題。當我嘗試導入其他模塊時,我不會收到此錯誤。導入python模塊失敗

~$ python 
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import jenkinsapi 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python2.6/site-packages/jenkinsapi/__init__.py", line 65, in <module> 
    __version__ = pkg_resources.working_set.by_key['jenkinsapi'].version 
KeyError: 'jenkinsapi' 
+0

你是如何安裝它的? – 2014-11-04 14:07:23

+0

什麼是您的操作系統,分配,CPU,系統等? – 2014-11-04 14:12:52

+0

pip安裝jenkinsapi,CentOS 6.5 vm(ESXi 5.5 vmware),內核2.6.32-431.20.3.el6.x86_64 – user2363318 2014-11-04 14:14:34

回答

5

Centos 6.4,Python 2.6.6,jenkinsapi 0.2.25通過點和獲得相同的錯誤。

我發現的是jenkinsapi遵守(延遲的)python標準pep-396,它似乎可能是該項目的貢獻者之一的this post的最新補充。

由於您的控制檯輸出顯示jenkinsapi的init正在嘗試從pkg_resources已知的設置中設置__version__。我能夠確認,在我的情況下,pkg_resources.working_set沒有加載jenkinsapi,儘管sys.path正確地包含了它所在的site-packages目錄。然而,像你一樣,它正在尋找我剛剛安裝的其他模塊。這對我來說真的好像是一個困惑的Python,所以 深入挖掘之前,我做了一個「點卸載jenkinsapi」和一個預感,我嘗試了文檔中列出的其他安裝方法; 「easy_install jenkinsapi」。
爲我修好了。

1

我面臨着類似的問題,並且現在使用unittest有一個不是很好的解決方法。

import unittest 
test_loader = unittest.defaultTestLoader.discover(start_dir= '.', pattern='test_controller.py') 
test_runner = unittest.runner.TextTestRunner() 
result = test_runner.run(test_loader).wasSuccessful() 

不好意思,但這意味着我可以調用一個存根,而該存根又調用從Git中提取的主代碼。 存根包含一個標準的unittest.testCase派生類。