简体   繁体   中英

How to install Python dependency properly? (maybe without sudo?)

I'm trying to run some code with python. It is using tweepy library. 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:

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 . I had a bad experience by using sudo for docker, because it creates protected files all over my local. 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

But, I see some warning to upgrade the pip, so I think maybe that's it. 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. Thanks.

If you use python3, you should be using pip3, pip is most likely the python2 pip.

However, better is using python3 -m pip install tweepy that ensures you use the pip for your specific python version.

You can also install it as a user without sudo for just your local account: 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. Use pip3 install tweepy instead. Maybe with sudo when you get the error with permission denied again. After that you can go with

python3 myscript.py

Use the --user flag, like so...

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

You seems to have 2 python installations on the machine. Python 3.x and 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

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 .

Like sudo pip3 install tweepy

If you want the pip to work as pip3 you can consider adding an alias with alias pip=pip3

You have to make sure the pip is pointing to right python version.

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