question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Netmiko and Textfsm Path and Env Issue Windows

See original GitHub issue

I am currently trying to set up a test with Netmiko and Textfsm in Windows 10, but no matter what path I try to setup the textfsm environment variable, it still doesn’t pick it up and throws an error:

Valid ntc-templates not found, please install https://github.com/networktocode/ntc-templates and then set the NET_TEXTFSM environment variable to point to the ./ntc-templates/templates directory.

I tried setting the environment variable manually via system properties --> environment variables, but still get the same message. I tried absolute as well as relative paths and no go. Ideally a relative path as the template folder will alway be alongside the script calling it. It might be something simple but im totally missing it right now.

The folder structure:

enter image description here

My Code:

import os, json
from netmiko import Netmiko
from netmiko import NetMikoAuthenticationException

templates = os.path.dirname(os.path.abspath(__file__)) + '\\ntc-template\\templates\\'
os.environ['NET_TEXTFSM']= templates
print(os.environ['NET_TEXTFSM'])

###############################################################
#Can i set the env var from within the scirpt using python?
#os.system(f'cmd /c "set NET_TEXTFSM={templates}"')
###############################################################

switch = {'device_type': 'cisco_ios',
            'ip': '192.168.0.20',
            'username': 'cisco',
            'password': 'cisco',
            'secret': 'cisco',
            'timeout': 10000,
            'session_timeout': 10000}

try:
    c = Netmiko(**switch)
    c.enable()
    show_ip_arp = c.send_command('show ip arp', use_textfsm=True)
    print(json.dumps(show_ip_arp))
except Exception as e:
    print(e)

I was hoping anyone points to what might be wrong or missing. I would love to avoid having to set up any environment variables via cmd unless it can be automated as well. The idea is whoever opens this py file gets all that’s needed to use textfsm.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Tes3awycommented, Sep 7, 2021

And does this directory exist?

C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\ntc_templates\\templates

Your error above is complaining that that directory doesn’t exist.

I would probably do the following:

pip uninstall ntc-templates
pip install ntc-templates

I finally found a solution to this issue:

  1. Go to Control Panel > View advanced system settings and click on Environment Variables.
  2. Under System variables (not User variables for username) click New and add the following variable: Variable name: NET_TEXTFSM Variable value: %APPDATA%\Python\Python39\site-packages\ntc_templates\templates
  3. Restart your text editor. If it didn’t work, restart your PC and try again.

I was able to get to this solution by removing ntc-templates (pip uninstall ntc-templates). When I run my code without ntc-templates, I get an error message suggesting a brief of what I presented here. And the solution works like a charm.

UPDATE:

I reproduced the error message to be available in the issue as well

Traceback (most recent call last):
  File "<PATH>", line 26, in <module>
    device_info = net_connect.send_command("show version", use_textfsm=True)[0]
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\netmiko\utilities.py", line 500, in wrapper_decorator
    return func(self, *args, **kwargs)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\netmiko\base_connection.py", line 1550, in send_command
    structured_output = get_structured_data(
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\netmiko\utilities.py", line 344, in get_structured_data
    template_dir = get_template_dir()
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\netmiko\utilities.py", line 297, in get_template_dir
    raise ValueError(msg)
ValueError:
Directory containing TextFSM index file not found.

Please set the NET_TEXTFSM environment variable to point at the directory containing your TextFSM
index file.

Alternatively, `pip install ntc-templates` (if using ntc-templates).

EDIT:

Sometime the Environment Variable unber System Variables only doesn’t work. Add the Env Var in both User and System Variables. However, Env Var under System Variables should be enough.

Thanks @ktbyers! Have a great day

1reaction
ktbyerscommented, Apr 16, 2020

Yes, you should only be using textfsm >= 1.1.0 at this point. Are you using the latest version of Netmiko (3.1.0)?

I think this should all work fine, but let me know if it isn’t.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Netmiko and Textfsm Path and Env Issue Windows
Issue resolved with the help of the netmiko repository owner: The code below works with the following libs and version:
Read more >
Netmiko and TextFSM - Python for Network Engineers
Now this is all fine, but how do we use TextFSM and ntc-templates with Netmiko. First we have to setup our environment properly....
Read more >
Netmiko And Textfsm Path And Env Issue Windows - ADocLib
I am running Python 3 on a Windows 10 computer. Netmiko where to look for the TextFSM template directory by setting the NETTEXTFSM...
Read more >
Netmiko textsfm windows 10 issue : r/networkautomation
I am running Python 3 on a Windows 10 computer. ... the TextFSM template directory by setting the NET_TEXTFSM environment variable (note, ...
Read more >
Text FSM with Netmiko - YouTube
Linux setup document: https://pynet.twb-tech.com/blog/automation/ netmiko - textfsm.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found