[英]How to install Python dependency properly? (maybe without sudo?)
I'm trying to run some code with python.我正在尝试使用 python 运行一些代码。 It is using tweepy library.
它正在使用tweepy库。 Then, I got this error:
然后,我得到了这个错误:
Traceback (most recent call last):
File "script.py", line 1, in <module>
import tweepy
ImportError: No module named 'tweepy'
So, I tried to install dependency: pip install tweepy
And it get permission denied:因此,我尝试安装依赖项:
pip install tweepy
并获得权限被拒绝:
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/sockshandler.py'
Consider using the `--user` option or check the permissions.
Next thing to do is to run using sudo
.接下来要做的是使用
sudo
运行。 I had a bad experience by using sudo
for docker, because it creates protected files all over my local.我对 docker 使用
sudo
的体验很糟糕,因为它会在我的本地创建受保护的文件。 But I finally tried it anyway sudo pip install tweepy
It returns success, but I still get the same error when I tried to run python3 myscript.py
但是我最终还是尝试了
sudo pip install tweepy
它返回成功,但是当我尝试运行python3 myscript.py
时仍然遇到相同的错误
But, I see some warning to upgrade the pip, so I think maybe that's it.但是,我看到一些升级 pip 的警告,所以我想也许就是这样。 I tried to upgrade pip using both
pip install --upgrade pip
and sudo pip install --upgrade pip
I tried to upgrade pip using both
pip install --upgrade pip
and sudo pip install --upgrade pip
Still not working.. I tried one last trick up my sleeve.仍然没有工作..我尝试了最后一个技巧。 Change the terminal.
换个终端。 I think, "maybe after installing, some environment variable not running on this terminal"
我想,“也许安装后,这个终端上没有运行一些环境变量”
Nope.没有。 Not working.
不工作。 I admit it should be a newbie question.
我承认这应该是一个新手问题。 Having tried some solution on the web, but still not working.
在 web 上尝试了一些解决方案,但仍然无法正常工作。 Thanks.
谢谢。
If you use python3, you should be using pip3, pip is most likely the python2 pip.如果你使用python3,你应该使用pip3,pip很可能是python2 pip。
However, better is using python3 -m pip install tweepy
that ensures you use the pip for your specific python version.但是,更好的是使用
python3 -m pip install tweepy
以确保您将 pip 用于您的特定 python 版本。
You can also install it as a user without sudo for just your local account: python3 -m pip install --user tweepy
您也可以将其安装为没有 sudo 的用户,仅用于您的本地帐户:
python3 -m pip install --user tweepy
Look at the error message:查看错误信息:
... Permission denied: '/usr/local/lib/python2.7 ....
You installed tweepy in your python2 Installation.您在 python2 安装中安装了 tweepy。 Use
pip3 install tweepy
instead.请改用
pip3 install tweepy
。 Maybe with sudo when you get the error with permission denied again.当您再次收到权限被拒绝的错误时,可能使用 sudo 。 After that you can go with
之后,您可以使用 go
python3 myscript.py
Use the --user
flag, like so...使用
--user
标志,像这样......
pip|pip3 install <PACKAGE> --user
This will install it in a location available and writeable to your user这会将其安装在您的用户可用且可写的位置
See https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site请参阅https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site
You seems to have 2 python installations on the machine.您似乎在机器上安装了 2 个 python。 Python 3.x and Python 2.7.
Python 3.x 和 Python 2.7。 When you run the
pip
command, the alias points to pip2
which installs packages for Python 2.7 - which is clear in your error message当您运行
pip
命令时,别名指向pip2
,它为 Python 2.7 安装软件包 - 这在您的错误消息中很清楚
Permission denied: '/usr/local/lib/python2.7/dist-packages/sockshandler.py'
So if you want to install packages for python 3, then use the command pip3
instead of pip
.因此,如果您想为 python 3 安装软件包,请使用命令
pip3
而不是pip
。
Like sudo pip3 install tweepy
像
sudo pip3 install tweepy
If you want the pip
to work as pip3
you can consider adding an alias with alias pip=pip3
如果您希望
pip
作为pip3
工作,您可以考虑添加别名为alias pip=pip3
You have to make sure the pip is pointing to right python version.您必须确保 pip 指向正确的 python 版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.