Python

Aaron West
Aaron West
256 Points

[pylint] Unable to import 'requests' [import-error] & Unable to import 'requests_ntlm'

I'm seeing this error: [pylint] Unable to import 'requests' [import-error] & Unable to import 'requests_ntlm' but I can do a pip freeze and get the following output (be advised that I'm just showing relevant output):

request==2018.11.20
requests==2.21.0
requests-ntlm==1.1.0

and my code that I'm playing around with is:

#!/usr/bin/env python

import requests
from requests_ntlm import HttpNtlmAuth

username = '<DOMAIN>\\<UserName>'
password = '<Password>'
tfsApi = 'https://{myserver}/tfs/collectionName/_apis/projects?api-version=2.0'

tfsResponse = requests.get(tfsApi,auth=HttpNtlmAuth(username,password))
if(tfsResponse.ok):
    tfsResponse = tfsResponse.json()
    print(tfsResponse)
else:
    tfsResponse.raise_for_status()

Python version: Python 3.7.0

Virtual env: ~/.envs/django/restapi/bin/python3

Update: only my debugger in VScode is giving me this error, but when I run the script it gets past this before it fails

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 25,205 Points

I don't know how helpful this is, but here are my thoughts.

From my experience, VSCode is not the easiest IDE to configure for virtual environments. I have to end up editing the settings JSON file any time I add a new virtual environment. Can you activate the specific environment you set up?

Here is the documentation (you probably already looked at this I would guess)

https://code.visualstudio.com/docs/python/environments

Also, try this-- from the command line, activate the virtual environment and run Python. Once you do that try to manually import "requests_ntlm". Did it succeed? With this, you could determine if the requests_ntlm was installed/compiled properly for the virtualenv you are using.

Aaron West
Aaron West
256 Points

Well, I actually just fixed the issue. I had to clean up a lot of my straggling interpreters that I had installed through pyenv.. It was making it very difficult to know and understand which interpreter that I had sourced because VS tried to source it for me I guess.

Is there a preferred IDE for dealing with virtual envs? I really like many of the VS plugins but I am always open to trying a new IDE if its a more reasonable one.

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 25,205 Points

Personally, I am partial to Wingware's Wing IDE. I like to set up my virtual environments with the older virtualenv on the command line, and then in the IDE, I point the project's interpreter at the Python installed in the virtualenv's bin folder.

It is good to have the command line skills, you appreciate it most when you are actually deploying on a remote server and all you have is a command line prompt!

But a great IDE that handles virtual environments very well is Pycharm. About all you need is their GUI to manage environments.

That being said, VSCode is pretty excellent once it is set up properly. Because it is smaller and simpler it quite a bit faster than PyCharm and more suited to low-resource machines.

Aaron West
Aaron West
256 Points

cool, I'll check out Wing IDE. I have used Pycharm in the past and actively elect to use virtualenv. I'm a command line kinda guy, so my favorite plugins are the some of the CLI tools that VS has (the code and vim/IDI plugins). the clutter that I had in VS was due to all of my old pyenv virtual environments that i still had installed, so I went back, reinstalled pyenv, and uninstalled all python versions that were there and that fixed it.