0

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.

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

An additional advice to complete this accurate answer: you could use a virtual environment (using python3 -m venv <path>) to simplify pip install calls and restrict them to the scope of a given project (when the venv is activated, you can safely use pip install, which will call the right pip and make the installation contextual to the venv).
I'm trying to use python3 -m pip install --user tweepy but getting this error /usr/bin/python3: No module named pip
owh, I think I should install pip3 first using sudo apt install python3-pip. And now it works.
0

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.