cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1322
Views
0
Helpful
2
Replies

Cobra SDK fails to import into python script

Wade Patterson
Cisco Employee
Cisco Employee

I'm trying to call modules from the cobra sdk into a python script but it keeps failing to load each time.  What I've done:

- downloaded both .whl files from the APIC (acicobra and acimodel)

- pip installed both .whl files into a python 3.8 virtual environment

- performed "run and debug" in VSC

 

Here is a screenshot of the failure during debug:

1838_898_1.png

 

I know that acicobra and acimodel is installed:

cobra installed.JPG

 

Not really sure why the script can't find the module.  Any help is greatly appreciated!!!! 

 

 

1 Accepted Solution

Accepted Solutions

kkhlebop
Cisco Employee
Cisco Employee

I don't see the error message in your post, perhaps you are referring to vscode's yellow squiggle underlines that refer to not found modules, so I will investigate that and try to recreate this issue.

 

I tried to recreate the issue with python 3.8.3 using these steps. In my case running python from the shell worked (which pointed at my virtual environment). However, when I opened with vscode, I had the same issue as you. This was, I found out, because vscode was trying to use another python environment and not the virtualenv. 

 

My suggestion is to look into how to get vscode to use the virtualenv instead of its default python paths. I might be wrong, but I don't think running source bin/activate is enough to get the rest of the IDE to use the same python version.

 

Here are the steps I took:

 

pyenv local 3.8.3    # switch to python 3.8.3

mkdir apic

cd apic

python -V; pip -V    # to confirm I am using correct version of python 3.8.3

python -m venv venv

source venv/bin/activate

python -V; pip -V    # to confirm that I am using the python 3.8.3 from the virtualenv

cp Downloads/aci* .    # copy the apic sdk files to local dir (i got these from https://developer.cisco.com/docs/aci/#!cobra-sdk-downloads/aci-learning-labs )

pip install acicobra-4.2_3h-py2.py3-none-any.whl    # install acicobra

pip install acimodel-4.2_3h-py2.py3-none-any.whl    # install acimodel

git clone https://github.com/CiscoDevNet/aci-learning-labs-code-samples

cd aci-learning-labs-code-samples/apic_fabric_setup    # go to the directory where startup file is at

code . # open vscode. then I saw the same marks - also trying to run the code from vscode fails at the import

python startup_script.py  # this code gets past the imports and fails at connection as I didn't edit my code's connection parms

### output: ###

❯ python startup_script.py
Baselining APIC Simulator for Learning Labs
Setting up Fabric Nodes
Traceback (most recent call last):
  File "startup_script.py", line 30, in <module>
    md.login()
  File "/Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages/cobra/mit/access.py", line 32, in login
    self._accessImpl.login()
  File "/Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages/cobra/internal/rest/accessimpl.py", line 128, in login
    loginHandler.login(self._session)
  File "/Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages/cobra/internal/rest/accessimpl.py", line 81, in login
    session._parseResponse(rsp)
  File "/Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages/cobra/mit/session.py", line 179, in _parseResponse
    rsp.raise_for_status()
  File "/Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages/requests/models.py", line 953, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://sandboxapicdc.cisco.com/api/aaaLogin.json

Here are my pip shows:

❯ pip show acimodel
Name: acimodel
Version: 4.2-3h
Summary: The Management Information Tree
Home-page: UNKNOWN
Author: Cisco Systems
Author-email: mtimm@cisco.com
License: Cisco Systems Inc. (Copyright 2014)
Location: /Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages
Requires: acicobra
Required-by:

❯ pip show acicobra
Name: acicobra
Version: 4.2-3h
Summary: Access API for the Management Information Tree
Home-page: UNKNOWN
Author: Cisco Systems
Author-email: gurssing@cisco.com
License: Cisco Systems Inc. (Copyright 2013 - 2014)
Location: /Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages
Requires: future, ply, requests, setuptools
Required-by: acimodel

Below is a screenshot showing the python paths and pip show for both modules - as you can see its pointing at wrong path and can't find it. If I do the source bin/activate, it will work for the terminal, but the rest of the IDE will not follow with - however I know there is a way, just need to look for vscode documentation or forums on how to get VSCODE working with virtualenv.

 

Screen Shot 2021-10-28 at 7.27.26 PM.png

 
 

 

View solution in original post

2 Replies 2

kkhlebop
Cisco Employee
Cisco Employee

I don't see the error message in your post, perhaps you are referring to vscode's yellow squiggle underlines that refer to not found modules, so I will investigate that and try to recreate this issue.

 

I tried to recreate the issue with python 3.8.3 using these steps. In my case running python from the shell worked (which pointed at my virtual environment). However, when I opened with vscode, I had the same issue as you. This was, I found out, because vscode was trying to use another python environment and not the virtualenv. 

 

My suggestion is to look into how to get vscode to use the virtualenv instead of its default python paths. I might be wrong, but I don't think running source bin/activate is enough to get the rest of the IDE to use the same python version.

 

Here are the steps I took:

 

pyenv local 3.8.3    # switch to python 3.8.3

mkdir apic

cd apic

python -V; pip -V    # to confirm I am using correct version of python 3.8.3

python -m venv venv

source venv/bin/activate

python -V; pip -V    # to confirm that I am using the python 3.8.3 from the virtualenv

cp Downloads/aci* .    # copy the apic sdk files to local dir (i got these from https://developer.cisco.com/docs/aci/#!cobra-sdk-downloads/aci-learning-labs )

pip install acicobra-4.2_3h-py2.py3-none-any.whl    # install acicobra

pip install acimodel-4.2_3h-py2.py3-none-any.whl    # install acimodel

git clone https://github.com/CiscoDevNet/aci-learning-labs-code-samples

cd aci-learning-labs-code-samples/apic_fabric_setup    # go to the directory where startup file is at

code . # open vscode. then I saw the same marks - also trying to run the code from vscode fails at the import

python startup_script.py  # this code gets past the imports and fails at connection as I didn't edit my code's connection parms

### output: ###

❯ python startup_script.py
Baselining APIC Simulator for Learning Labs
Setting up Fabric Nodes
Traceback (most recent call last):
  File "startup_script.py", line 30, in <module>
    md.login()
  File "/Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages/cobra/mit/access.py", line 32, in login
    self._accessImpl.login()
  File "/Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages/cobra/internal/rest/accessimpl.py", line 128, in login
    loginHandler.login(self._session)
  File "/Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages/cobra/internal/rest/accessimpl.py", line 81, in login
    session._parseResponse(rsp)
  File "/Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages/cobra/mit/session.py", line 179, in _parseResponse
    rsp.raise_for_status()
  File "/Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages/requests/models.py", line 953, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://sandboxapicdc.cisco.com/api/aaaLogin.json

Here are my pip shows:

❯ pip show acimodel
Name: acimodel
Version: 4.2-3h
Summary: The Management Information Tree
Home-page: UNKNOWN
Author: Cisco Systems
Author-email: mtimm@cisco.com
License: Cisco Systems Inc. (Copyright 2014)
Location: /Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages
Requires: acicobra
Required-by:

❯ pip show acicobra
Name: acicobra
Version: 4.2-3h
Summary: Access API for the Management Information Tree
Home-page: UNKNOWN
Author: Cisco Systems
Author-email: gurssing@cisco.com
License: Cisco Systems Inc. (Copyright 2013 - 2014)
Location: /Users/kkhlebop/Dropbox/src/apic/venv/lib/python3.8/site-packages
Requires: future, ply, requests, setuptools
Required-by: acimodel

Below is a screenshot showing the python paths and pip show for both modules - as you can see its pointing at wrong path and can't find it. If I do the source bin/activate, it will work for the terminal, but the rest of the IDE will not follow with - however I know there is a way, just need to look for vscode documentation or forums on how to get VSCODE working with virtualenv.

 

Screen Shot 2021-10-28 at 7.27.26 PM.png

 
 

 

Wade Patterson
Cisco Employee
Cisco Employee

Very helpful and thank you for the detailed post @kkhlebop !!!  The only thing I have to add is that I needed to change the url/username/password in the credentials.py script to a different ACI environment.