简体   繁体   中英

python called from makefile can't find urllib.request

This seemed as a typical python2->3 error at first, but I wasn't able to figure out what could be going wrong here. Might have to do with a conflict between conda and the makefile.

In my makefile , I have a rule:

update_stuff:
    python --version
    which python
    python stuff.py

which outputs

python --version
Python 3.8.3
which python
/Users/username/anaconda3/bin/python
...

then in my python script, I have those relevant lines:

import urllib
import sys
print(sys.version)
print(sys.executable)
req = urllib.request.Request(URL)

which outputs first

3.8.3 (default, Jul  2 2020, 11:26:31) 
[Clang 10.0.0 ]
/Users/user/anaconda3/bin/python

but then gives me an error

  File "stuff.py", line 278, in <module>
    req = urllib.request.Request(URL)
AttributeError: module 'urllib' has no attribute 'request'
make: *** [update_stuff] Error 1

If I just run the file from the command line with python stuff.py , it works fine and I cannot see where a python version might be called that doesn't know urllib . Running make SHELL=/bin/bash changes nothing. Any pointers to how to fix it?

You need to specify import:

import urllib.request

https://docs.python.org/3.8/library/urllib.request.html#module-urllib.request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM