0

I have installed both requests and requests_ntlm modules using "sudo python3 -m pip install requests" (and requests_ntlm respectively) and both installs were successful.

When I then attempt to do "from requests import HttpNtlmAuth", I get an error stating "cannot import name 'HttpNtlmAuth'. I do not get this error on my "import requests" line.

When I do a "sudo python3 -m pip list", I see both are installed and are the latest versions.

I've not encountered this error before, only "cannot import module", so I'm unfamiliar with how to resolve this.

EDIT 1: Additional information. When I run this script from command line as "sudo", it works. Because I am running my python script from within a PHP file using "exec", I don't particularly want to run this as a root user. Is there a way around this, or possibly running the exec statement with sudo?

Source Matters
  • 1,110
  • 2
  • 15
  • 35
  • On to something! So I noticed that when I run my script with "sudo", it works fine. The way I run my script is from within a PHP file. Is it normal to have to run this as "sudo"? – Source Matters Oct 20 '18 at 19:18
  • Not sure, but if you are running it from a PHP file maybe it is being execued by www-data user, or other depending on your configuration. In that case maybe you need to install the modules for that user, or ensure it has access. Check this link: https://stackoverflow.com/questions/39471295/how-to-install-python-package-for-global-use-by-all-users-incl-www-data Also, is php using python3 to run the script? maybe it is using python2. – Santiago Bruno Oct 20 '18 at 19:40
  • You should not need to run sudo if you are using a virtual env and you should use a virtual env to isolate any upgrades to one app. `python3 -m venv /tmp/ve` will install the env in /tmp/ve you then use `/tmp/ve/bin/pip install requests` your app will have no problems doing it this way. PS to run a script with this ve `/tmp/ve/bin/python myscript.py` or you can include `#!/tmp/ve/bin/python` in your script's shebang line. – Peter Moore Oct 24 '19 at 14:40

1 Answers1

1

the HttpNtlmAuth class is in the requests_ntlm package so you'll need to have:

import requests
from requests_ntlm import HttpNtlmAuth

Then you'll be able to instantiate your authentication

session = requests.Session()
session.auth = HttpNtlmAuth('domain\\username','password')
session.get(url)
scign
  • 812
  • 5
  • 15