python 从 makefile 调用找不到 urllib.request

python called from makefile can't find urllib.request

起初这似乎是一个典型的 python2->3 错误,但我无法弄清楚这里可能出了什么问题。可能与 conda 和 makefile 之间的冲突有关。

在我的makefile中,我有一个规则:

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

输出

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

然后在我的 python 脚本中,我有那些相关的行:

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

哪个先输出

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

然后给我一个错误

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

如果我只是 运行 从命令行使用 python stuff.py 文件,它工作正常,我看不到 python 版本可能在哪里被调用,不知道 urllib。 运行 make SHELL=/bin/bash 没有任何改变。关于如何修复它的任何指示?

您需要指定导入:

import urllib.request

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