0

I am trying to use python to interact with jenkins.

Initially when I tried to import jenkinsapi , I got the below ERROR.

>>> import jenkinsapi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/jenkinsapi-0.3.4/jenkinsapi/__init__.py", line 49, in <module>
    from jenkinsapi import (
  File "/root/jenkinsapi-0.3.4/jenkinsapi/api.py", line 15, in <module>
    from jenkinsapi.jenkins import Jenkins
  File "/root/jenkinsapi-0.3.4/jenkinsapi/jenkins.py", line 12, in <module>
    from jenkinsapi.jobs import Jobs
  File "/root/jenkinsapi-0.3.4/jenkinsapi/jobs.py", line 6, in <module>
    from jenkinsapi.job import Job
  File "/root/jenkinsapi-0.3.4/jenkinsapi/job.py", line 10, in <module>
    from jenkinsapi.build import Build
  File "/root/jenkinsapi-0.3.4/jenkinsapi/build.py", line 17, in <module>
    import pytz
ImportError: No module named 'pytz'

Then I installed pytz.

# yum install python34-pytz.noarch -y
Loaded plugins: product-id, rhnplugin, search-disabled-repos, subscription-manager
This system is receiving updates from RHN Classic or Red Hat Satellite.
Resolving Dependencies
--> Running transaction check
---> Package python34-pytz.noarch 0:2016.7-1.el7 will be installed
--> Processing Dependency: python(abi) = 3.4 for package: python34-pytz-2016.7-1.el7.noarch
--> Running transaction check
---> Package python34.x86_64 0:3.4.5-3.el7 will be installed
--> Processing Dependency: python34-libs(x86-64) = 3.4.5-3.el7 for package: python34-3.4.5-3.el7.x86_64
--> Processing Dependency: libpython3.4m.so.1.0()(64bit) for package: python34-3.4.5-3.el7.x86_64
--> Running transaction check
---> Package python34-libs.x86_64 0:3.4.5-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================================================================================
 Package                                                               Arch                                                           Version                                                              Repository                                                    Size
==============================================================================================================================================================================================================================================================================
Installing:
 python34-pytz                                                         noarch                                                         2016.7-1.el7                                                         epel                                                          46 k
Installing for dependencies:
 python34                                                              x86_64                                                         3.4.5-3.el7                                                          epel                                                          50 k
 python34-libs                                                         x86_64                                                         3.4.5-3.el7                                                          epel                                                         6.7 M

Transaction Summary
==============================================================================================================================================================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 6.8 M
Installed size: 28 M
Downloading packages:
(1/3): python34-3.4.5-3.el7.x86_64.rpm                                                                                                                                                                                                                 |  50 kB  00:00:00     
(2/3): python34-pytz-2016.7-1.el7.noarch.rpm                                                                                                                                                                                                           |  46 kB  00:00:00     
(3/3): python34-libs-3.4.5-3.el7.x86_64.rpm                                                                                                                                                                                                            | 6.7 MB  00:00:01     
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                                                         4.2 MB/s | 6.8 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python34-3.4.5-3.el7.x86_64                                                                                                                                                                                                                                1/3 
  Installing : python34-libs-3.4.5-3.el7.x86_64                                                                                                                                                                                                                           2/3 
  Installing : python34-pytz-2016.7-1.el7.noarch                                                                                                                                                                                                                          3/3 
  Verifying  : python34-pytz-2016.7-1.el7.noarch                                                                                                                                                                                                                          1/3 
  Verifying  : python34-libs-3.4.5-3.el7.x86_64                                                                                                                                                                                                                           2/3 
  Verifying  : python34-3.4.5-3.el7.x86_64                                                                                                                                                                                                                                3/3 

Installed:
  python34-pytz.noarch 0:2016.7-1.el7                                                                                                                                                                                                                                         

Dependency Installed:
  python34.x86_64 0:3.4.5-3.el7                                                                                                       python34-libs.x86_64 0:3.4.5-3.el7                                                                                                      

Complete!

This resulted in successful import of jenkinsapi

root@dselilx6390 ~/jenkinsapi-0.3.4
# python3.4
Python 3.4.5 (default, Nov  9 2016, 16:24:59) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import jenkinsapi
>>> 

I was able to do a lot of work and then logged out of python CLI. I then logged back in and tried to import jenkinsapi and then got the ERROR.

>>> import jenkinsapi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'jenkinsapi'

All of a sudden, its giving me an ImportError: No module named 'jenkinsapi' error. I have not installed any packages or modified anything. What could have gone wrong ?

  • If you close Python shell (`quit()`) and issue command `whoami`, what is the output? – Mirek Długosz Apr 19 '17 at 21:28
  • @MirosławZalewski `# python3.4 Python 3.4.5 (default, Nov 9 2016, 16:24:59) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> quit() root@dselilx6390 ~ # whoami root` –  Apr 19 '17 at 21:28
  • OK, that looks good. What about `pwd` output? – Mirek Długosz Apr 19 '17 at 21:30
  • @MirosławZalewski `# pwd /root ` –  Apr 19 '17 at 21:31

1 Answers1

0

The problem at hand is that when you were able to import jenkinsapi, your working directory was ~/jenkinsapi-0.3.4; after log out and log in, your working directory is ~. In latter case, Python is unable to find module in the same directory and fails.

If you do cd ~/jenkinsapi-0.3.4 and then open Python shell, import jenkinsapi should work.

As a side note, you might want to learn about Pythons virtual environments (see this StackOverflow question for pointers) and installing packages through pip.

Community
  • 1
  • 1
Mirek Długosz
  • 4,205
  • 3
  • 24
  • 41