Junos Automation (Scripting)
Highlighted
Junos Automation (Scripting)

Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 03:00 AM

I am using Windows and  trying to run this python code as mentioned below but unable to get past this error:

 

from jnpr.junos import Device
ModuleNotFoundError: No module named 'jnpr'

 

The script is pretty straightforward as outlined in document below:

 

https://kb.juniper.net/InfoCenter/index?page=content&id=KB34524&cat=MX_SERIES&actp=LIST&showDraft=fa...

In the following example, we are connecting to the address: 10.1.1.1, with the user root and Password: test123​ using Python Script:

from pprint import pprint
from jnpr.junos import Device
from jnpr.junos.utils.start_shell import StartShell
hostname= Device(host='10.1.1.1', user='root', password='test123
')
ss = StartShell(hostname)
ss.open()
hostname.open()
pprint(ss.run('cli -c "show version | no-more"'))       # <-- For regular CLI commands
pprint(ss.run('cprod -A fpc0 -c "show route summary"')) # <-- For VTY commands
hostname.close()
ss.close()

Save the above contents as ".py" file and run it using the following command:

python3 filename.py

You can also save the outputs to a file instead of printing them on the console:

python3 filename.py> save logfile.txt

 

 

 

 

 

 

21 REPLIES 21
Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 03:05 AM

Hi Simon, 

 

Greetings, 

 

This error is because of reason that the module not installed and you are trying to use it. 

Use this link : https://www.juniper.net/documentation/en_US/junos-pyez/topics/task/troubleshooting/junos-pyez-import...

 

Hope this helps. Smiley Happy

 

Please mark "Accept as solution" if this answers your query. 

 

Kudos are appreciated too! 

 

Regards, 

Sharat Ainapur

Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 03:14 AM

That is the first thing that I have tried but it didnt work. 

I have tried this on python3 and I get the following error:

python3
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import jnpr.junos
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'jnpr'
>>> import jnpr.junos
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'jnpr'

 

 

I also used Pycharm but that fails on error too:

line 2, in <module>
from jnpr.junos import Device
ModuleNotFoundError: No module named 'jnpr'

 

Any help would be much appreciated. 

 

Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 03:25 AM

Can you please execute the following command:

[sudo] pip install junos-eznc

 

After the installation is finished successfully, "import jnpr.junos" should work.

--------------------------------------------------

If this solves your problem, please mark this post as "Accepted Solution".
If you think that my answer was helpful, please spend some Kudos.
Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 03:36 AM

This has already been done too and getting below error:

 

>>> import jnpr.junos
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'jnpr'
>>>

 

 

 

 

Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 03:38 AM

Can you please share the complete log of your "pip install junos-eznc" installation, as this works fine for me.

--------------------------------------------------

If this solves your problem, please mark this post as "Accepted Solution".
If you think that my answer was helpful, please spend some Kudos.
Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 04:10 AM

Hi Simon, 

 

Yes, you are right, seems to be a known issue on the Web.

As per my understanding something is blocking this on your device. This might as well be due to antivirus/firewall. Verify if the project settings has the module(jnpr) installed in PyCharm also

 

Refer this python forum thread for a few steps to resolve this. Bypassing the Firewall or running as admin might be of some help:  https://python-forum.io/Thread-Import-Error-jnpr-junos

 

Let me know if this works.  Hope this helps.

 

Please mark "Accept as solution" if this solves your query. 

Kudos are appreciated too! 

 

Regards, 

Sharat Ainapur

Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 07:29 AM

Here are logs switched off antivirus and firewall but still same jncr error

 

 

pip install junos-eznc
Requirement already satisfied: junos-eznc in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (2.4.1)
Requirement already satisfied: PyYAML>=5.1 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (5.3.1)
Requirement already satisfied: transitions in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (0.8.1)
Requirement already satisfied: netaddr in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (0.7.19)
Requirement already satisfied: yamlordereddictloader in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (0.4.0)
Requirement already satisfied: jinja2>=2.7.1 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (2.11.2)
Requirement already satisfied: pyserial in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (3.4)
Requirement already satisfied: lxml>=3.2.4 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (4.5.1)
Requirement already satisfied: ntc-templates in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (1.5.0)
Requirement already satisfied: pyparsing in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (2.4.7)
Requirement already satisfied: ncclient>=0.6.3 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (0.6.7)
Requirement already satisfied: scp>=0.7.0 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (0.13.2)
Requirement already satisfied: paramiko>=1.15.2 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (2.7.1)
Requirement already satisfied: six in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (1.15.0)
Requirement already satisfied: MarkupSafe>=0.23 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from jinja2>=2.7.1->junos-eznc) (1.1.1)
Requirement already satisfied: textfsm>=1.1.0 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from ntc-templates->junos-eznc) (1.1.0)
Requirement already satisfied: setuptools>0.6 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from ncclient>=0.6.3->junos-eznc) (41.2.0)
Requirement already satisfied: cryptography>=2.5 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from paramiko>=1.15.2->junos-eznc) (2.9.2)
Requirement already satisfied: bcrypt>=3.1.3 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from paramiko>=1.15.2->junos-eznc) (3.1.7)
Requirement already satisfied: pynacl>=1.0.1 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from paramiko>=1.15.2->junos-eznc) (1.4.0)
Requirement already satisfied: future in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from textfsm>=1.1.0->ntc-templates->junos-eznc) (0.18.2)
Requirement already satisfied: cffi!=1.11.3,>=1.8 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from cryptography>=2.5->paramiko>=1.15.2->junos-eznc) (1.14.0)
Requirement already satisfied: pycparser in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from cffi!=1.11.3,>=1.8->cryptography>=2.5->paramiko>=1.15.2->junos-eznc) (2.20)

Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 07:46 AM

There seems to be indeed any issue with the junos-eznc package. It worked for me with Python 3.7.3, then I upgraded to 3.8.3 and loaded the junos-eznc again, now I get the error message:

 

>>> import jnpr.junos
Traceback (most recent call last):
File "<stdin>", line 1, in <module>

[...]

ModuleNotFoundError: No module named 'fcntl'

--------------------------------------------------

If this solves your problem, please mark this post as "Accepted Solution".
If you think that my answer was helpful, please spend some Kudos.
Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 10:20 AM

I have managed to successfully connect to my router but using Linux not  Windows. think there is an issue with windows and jnpr.junos. 

 

My next query is how to I make my output look user friendly as its all fudged up.from pprint import pprint

I also need to run command show chassis hardware to get s/no of unit but that is not showing either. 

 

Please anyone help.

 

 

from pprint import pprint
from jnpr.junos import Device
from jnpr.junos.utils.start_shell import StartShell
hostname= Device(host=1.1.1.1, user='root', password='eb08fb5d')
ss = StartShell(hostname)
ss.open()
hostname.open()
pprint(ss.run('cli -c "show version | no-more"')) # <-- For regular CLI commands
pprint(ss.run('cprod -A fpc0 -c "show route summary"')) # <-- For VTY commands
hostname.close()
ss.close()

Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 12:54 PM

Please execute this on your Windows environment:

 

pip install git+https://github.com/Juniper/py-junos-eznc.git


After performing this it should work. At least it worked for me.

--------------------------------------------------

If this solves your problem, please mark this post as "Accepted Solution".
If you think that my answer was helpful, please spend some Kudos.
Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 02:12 PM

It appears my code only runs on old python 2.7.8 if anything above 3 is used it fails.

 

I have tried to run pip install git+https://github.com/Juniper/py-junos-eznc.git but getting below error:

 

 

ERROR: Error [WinError 2] The system cannot find the file specified while executing command git clone -q https://github.com/Juniper/py-junos-eznc.git 'C:\Users\Jazz\AppData\Local\Temp\pip-req-build-7sktmot3'
ERROR: Cannot find command 'git' - do you have 'git' installed and in your PATH?

 

Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-18-2020 04:20 PM

Can you please check what pip version is installed using  command "pip -V" ? If the pip version isn't inline with the python version you are using with the script then please try using pip3 while installing pyez package



Regards,
Nupur Kanoi
Juniper Ambassador
JNCIE-ENT#520, JNCIE-SP#2824, JNCIP-DC & Juniper Ingenious Champion
Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-19-2020 01:55 AM

Make sure that you've just installed Python 3 (doesn't matter if 3.7 or 3.8)

 

At first, if not already present, install GIT for Windows:
https://git-scm.com/download/win

 

After that, reboot your device.

 

Then try to upgrade pip:
python -m pip install --upgrade pip


After that execute the download of junos-eznc again:
pip install git+https://github.com/Juniper/py-junos-eznc.git

 

After that it should work:

> python
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import jnpr.junos
>>>

--------------------------------------------------

If this solves your problem, please mark this post as "Accepted Solution".
If you think that my answer was helpful, please spend some Kudos.
Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-21-2020 11:52 AM

Many thanks Sir your solution with git has saved me.

 

I have run this code now and it works however indentation is not great  with lots of \r\n etc 

 

 

(True,
'cli -c "show version | no-more"\r'
'\r\n'
'Hostname: Test001-001\r\n'
'Model: srx300\r\n'
'Junos: 15.1X49-D150.2\r\n'
'JUNOS Software Release [15.1X49-D150.2]\r\n'
'root@Test001-001% ')
(False,
'cprod -A fpc0 -c "show route summary"\r'
'\r\n'
'================ scb ================\r\n'
'Broken pipe\r\n'

 

Any ideas how to get rid of all these \r\n to make out look bit pretty?

 

 

Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-21-2020 12:21 PM

Hello Simon,

 

please check if this helps you to get rid of these characters:

https://stackoverflow.com/questions/13188244/python-get-rid-of-unicode-and-r-n-characters

 

https://stackoverflow.com/questions/45383938/cant-delete-r-n-from-a-string/45383951

 

Please be so kind and click to "Accept Solution" to my former post so that others can find it very quickly. Thank you very much.

--------------------------------------------------

If this solves your problem, please mark this post as "Accepted Solution".
If you think that my answer was helpful, please spend some Kudos.
Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-21-2020 10:32 PM

Hi Simon

 

The startshell function actually returns a tuple with the first element as True/False and second element as the output of the command.

You can try to print the second element of the tuple to print only the output.

Also you can use the links suggested by F1ght3r to remove /r/n from the output.

 

 

Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-25-2020 02:52 AM

I have a requirement to logon to juniper devices using a port other than ssh 22 - This mean to ssh but using port say 8022 instead. 

How do I edit this code or has any one got some examples:

 

from pprint import pprint
from jnpr.junos import Device
from jnpr.junos.utils.start_shell import StartShell
hostname= Device(host='10.1.1.1', user='root', password='test123
')  ** port required is 8022 instead of 22**
ss = StartShell(hostname)
ss.open()
hostname.open()
pprint(ss.run('cli -c "show version | no-more"'))       # <-- For regular CLI commands
pprint(ss.run('cprod -A fpc0 -c "show route summary"')) # <-- For VTY commands
hostname.close()
ss.close()

Save the above contents as ".py" file and run it using the following command:

python3 filename.py

Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-25-2020 03:09 AM

Hi Simon

 

You can use the port parameter from the Pyez and that should work.

 

hostname= Device(host='10.1.1.1', user='root', password='test123 , port = 8022')

 

By default the port will be 22.

Make sure that device also has the same port configuration for ssh otherwise the connection will be refused.

 

Hope this helps.

Highlighted
Junos Automation (Scripting)

Re: Juniper Script error from jnpr.junos import Device ModuleNotFoundError: No module named 'jnpr'

‎06-25-2020 04:01 AM

I get an error 

 

retry_on_signal(lambda: sock.connect(addr))
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

 

 

This works fine if you make ssh connection using port 8022 though.

Feedback