Python 已经安装了 python 的 jenkins 但是在 pycharm 中无法使用,报 AttributeError: module 'jenkins' has no attribute 'Jenkins'

咸吃萝卜淡操心 · February 27, 2019 · Last by 咸吃萝卜淡操心 replied at February 27, 2019 · 3244 hits

使用 pip3 install python-jenkins 后在 pycharm 中使用,运行以后报错,但是在命令行使用就没问题,已经确认 pycharm 环境中有 python-jenkins

import jenkins
jenkins_server_url='http://localhost:8080'
user_id='admin'
api_token='admin'
server = jenkins.Jenkins('http://localhost:8080', username=user_id, password=api_token)

Traceback (most recent call last):
  File "/Users/xxxx/PycharmProjects/untitled/jenkins.py", line 3, in <module>
    import jenkins
  File "/Users/xxxx/PycharmProjects/untitled/jenkins.py", line 16, in <module>
    server = jenkins.Jenkins('http://localhost:8080', username=user_id, password=api_token)
AttributeError: module 'jenkins' has no attribute 'Jenkins'

共收到 3 条回复 时间 点赞

你的文件名不应该和模块名重复,否则导入的时候会找不到

jenkins.py 这个文件不要和已存在的模块 jenkins 重名

楼上正解,模块名不能和文件名重名,否则会报错。

Jerry li 回复

谢谢解答,一直没注意过这方面的东西,又学到了

咸吃萝卜淡操心 关闭了讨论 27 Feb 13:17
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up