[英]Module found on python shell / interactive prompt, but getting ModuleNotFound on import in file
I am trying to use python-twitter
( https://python-twitter.readthedocs.io/en/latest/index.html ), and I am not having much luck.我正在尝试使用
python-twitter
( https://python-twitter.readthedocs.io/en/latest/index.html ),但我运气不佳。 The python-twitter
module is invoked with import twitter
, unfortunately when I attempt to do that via my script from the command line it isn't working.使用
import twitter
调用python-twitter
模块,不幸的是,当我尝试从命令行通过我的脚本执行此操作时,它不起作用。
What's boggling my mind, is that there is no similar error when executing from the python shell.令人难以置信的是,从 python shell 执行时没有类似的错误。
Everything is installed correctly, it would seem:一切都正确安装,看起来:
(venv) user@computer:~/Documents/projectdir$ pip3 install python-twitter
Requirement already satisfied: python-twitter in /home/user/.local/lib/python3.8/site-packages (3.5)
Requirement already satisfied: requests-oauthlib in /home/user/.local/lib/python3.8/site-packages (from python-twitter) (1.3.0)
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (from python-twitter) (2.22.0)
Requirement already satisfied: future in /usr/lib/python3/dist-packages (from python-twitter) (0.18.2)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/lib/python3/dist-packages (from requests-oauthlib->python-twitter) (3.1.0)
(venv) user@computer:~/Documents/projectdir$ pip3 install twitter
Requirement already satisfied: twitter in /home/user/.local/lib/python3.8/site-packages (1.18.0)
(venv) user@computer:~/Documents/projectdir$ pip3 freeze > requirements.txt
(venv) user@computer:~/Documents/projectdir$ echo "$(<requirements.txt)"
...
python-twitter==3.5
...
twitter==1.18.0
...
And yet my script throws an error:然而我的脚本抛出了一个错误:
(venv) user@computer:~/Documents/projectdir$ sudo python3 authentication_test.py
Traceback (most recent call last):
File "authentication_test.py", line 1, in <module>
import twitter
ModuleNotFoundError: No module named 'twitter'
Despite the twitter
module being found by the python shell:尽管 python shell 发现了
twitter
模块:
(venv) user@computer:~/Documents/projectdir$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import twitter
>>> twitter.Api()
<twitter.api.Api object at 0x7f11b0056a60>
>>>
What's going on here?这里发生了什么? I thought maybe there was something causing issues between
twitter
and python-twitter
, but I did pip3 uninstall twitter
and the issue persists.我认为
twitter
和python-twitter
之间可能存在问题,但我确实pip3 uninstall twitter
并且问题仍然存在。
I should note that although I broke them up with comments, the above code snippets were done consecutively.我应该注意,虽然我用注释将它们分开,但上面的代码片段是连续完成的。
Ideas?想法?
As stated by Aaron (thank you), the answer was that sudo
was taking me out of the virtualenv
context, and thus the packages were not available.正如 Aaron 所说(谢谢),答案是
sudo
让我脱离了virtualenv
上下文,因此这些包不可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.