Cannot pip install package in virtualenv on EC2

2024/10/10 0:24:20

I'm seeing this weird issue on ec2. I'm trying to install lsm-db package inside my virtualenv, it says it's successfully installed but when trying to import the package or do pip list it's not there.

I created this virtualenv by

virtualenv -p python3.6 venv
source venv/bin/activate

then running which python and which pip shows the correct ones inside venv as <path-to-my-project>/venv/bin/pip. Then I run pip install -r requirements.txt where sqlalchemy and lsm-db are included. A warning is shown

WARNING: Building wheel for lsm-db failed: [Errno 13] Permission denied: '/home/ec2-user/.cache/pip'WARNING: Building wheel for sqlalchemy failed: [Errno 13] Permission denied: '/home/ec2-user/.cache/pip'
Failed to build lsm-db sqlalchemy
Installing collected packages: sqlalchemy, flask-sqlalchemy, lsm-db, MarkupSafe, clickRunning setup.py install for sqlalchemy ... doneRunning setup.py install for lsm-db ... done
Successfully installed MarkupSafe-1.1.1 click-7.0 flask-sqlalchemy-2.4.0 lsm-db-0.6.4 sqlalchemy-1.3.5

The last line shows they are correctly installed despite the warnings. However, they are not. Running pip install lsm-db gives

▶ pip install lsm-db
WARNING: The directory '/home/ec2-user/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
WARNING: The directory '/home/ec2-user/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting lsm-dbDownloading https://files.pythonhosted.org/packages/17/f2/dc68ecdece424eb206c42b8db924c5a7f6dd97d26df42a1c9fb41a6fe555/lsm-db-0.6.4.tar.gz (316kB)|████████████████████████████████| 317kB 14.7MB/s
Building wheels for collected packages: lsm-dbWARNING: Building wheel for lsm-db failed: [Errno 13] Permission denied: '/home/ec2-user/.cache/pip'
Failed to build lsm-db
Installing collected packages: lsm-dbRunning setup.py install for lsm-db ... done
Successfully installed lsm-db-0.6.4

Again it shows it's installed, but it's not. Not in pip list, can't import in Python. What is going on here? Why do I see permission issue inside virtualenv? I didn't create the virtualenv with sudo if that's a potential problem. Is it related to the way I installed virtualenv?


EDIT

I changed the permission and reinstall pip with sudo yum install python36-pip. Now I'm seeing this... sorry it seems like a new problem

Running setup.py install for lsm-db ... errorERROR: Complete output from command /home/ec2-user/<project>/venv/bin/python3.6 -u -c 'import setuptools, tokenize;__file__='"'"'/tmp/pip-install-bhhmd3j8/lsm-db/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-__gotpo6/install-record.txt --single-version-externally-managed --compile --install-headers /home/ec2-user/<project>/venv/include/site/python3.6/lsm-db:ERROR: /tmp/pip-install-bhhmd3j8/lsm-db/setup.py:11: UserWarning: Cython not installed, using pre-generated C source file.warnings.warn('Cython not installed, using pre-generated C source file.')running installrunning buildrunning build_extbuilding 'lsm' extensioncreating buildcreating build/temp.linux-x86_64-3.6creating build/temp.linux-x86_64-3.6/srcgcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/python3.6m -c lsm.c -o build/temp.linux-x86_64-3.6/lsm.olsm.c:32:20: fatal error: Python.h: No such file or directory#include "Python.h"^compilation terminated.error: command 'gcc' failed with exit status 1----------------------------------------
ERROR: Command "/home/ec2-user/<project>/venv/bin/python3.6 -u -c 'import setuptools, tokenize;__file__='"'"'/tmp/pip-install-bhhmd3j8/lsm-db/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-__gotpo6/install-record.txt --single-version-externally-managed --compile --install-headers /home/ec2-user/<project>/venv/include/site/python3.6/lsm-db" failed with error code 1 in /tmp/pip-install-bhhmd3j8/lsm-db/

EDIT

Thanks for the pointers, now I installed the python development package on ec2 and tried to install lsm-db again

> sudo yum install python36-devel
...
> pip install lsm-db
Collecting lsm-dbUsing cached https://files.pythonhosted.org/packages/17/f2/dc68ecdece424eb206c42b8db924c5a7f6dd97d26df42a1c9fb41a6fe555/lsm-db-0.6.4.tar.gz
Building wheels for collected packages: lsm-dbBuilding wheel for lsm-db (setup.py) ... doneStored in directory: /home/ec2-user/.cache/pip/wheels/ed/32/8a/b0f3aa15b68f1e6b8f2dcc6285de4796b6b58af71c5f280f48
Successfully built lsm-db
Installing collected packages: lsm-db
Successfully installed lsm-db-0.6.4

again, it shows it's successful, and which python gives me the right python in my venv. However, I have the exact same result as the beginning, pip list does not have lsm-db and I cannot import it in python.

This is entirely an issue on ec2, I can successfully use lsm-db in my virtualenv on my mac. There must be something off with virtualenv on ec2 here, why the right pip in venv cannot install a package in venv?? I'm completely lost. If anyone has experience please chime in!

(To work around this problem, I'm switching from lsm-db to sqlitedict in my code. sqlitedict can be installed no problem, so it narrows the suspect down to just lsm-db specific issues.)

This question from 2016 is more similar to mine but it has no conclusion why or any solution. Pip says that pymongo has been installed, but doesn't show it as installed

Another potentially related question: Amazon EC2 virtualenv: pip says it installed numpy but python can't find it

Answer

The problem seems to be with system file permissions. You can check the owner:group as well as rwx permissions for other users of the folder '/home/ec2-user/.cache/pip' from terminal using:

ls -lh /home/ec2-user/.cache/pip

In order to change the owner of the folder use command:

sudo chown -R ec2-user /home/ec2-user/.cache/pip

Another way to solve is to allow rwx permissions to all users by:

sudo chmod -R 777 /home/ec2-user/.cache/pip

https://en.xdnf.cn/q/118521.html

Related Q&A

Python: string to integer as a key

Im trying to convert a string column in a dataframe to int. The strings should be replaced with an integer as a key value.Data:user_id site_id 100 url1.com 100 url2.com 100 url1.com 101…

Data manipulation, kind of downsampling

I have a large csv file, example of the data below. I will use an example of eight teams to illustrate.home_team away_team home_score away_score year belgium france 2…

Chrome Native Messaging throwing error when sending a base64 string to client

Using Chrome Native Messaging sample app as a template am able make a system call to bashos.system("<bash command>")The requirement is to return a base64 string from the python scriptos…

Exporting DataFrame to Excel using pandas without subscribe

How can I export DataFrame to excel without subscribe? For exemple: Im doing webscraping and there is a table with pagination, so I take the page 1 save it in DataFrame, export to excel e do it again …

Fraction of a real number in python giving complicated answer

Importing Fraction from fractions to give a fractional representation of a real number, but giving responses quite complicated which seems very simple by the paper-pen method. Fractions(.2) giving answ…

Scrape latitude and longitude (Google Maps) inside Script type=text/javascript

Im beginner in Web Scrapping. Im trying to get latitude and longitude from this web: https://urbania.pe/inmueble/proyecto/ememhvin-proyecto-mariscal-castilla-lima-santiago-de-surco-tale-inmobiliaria-65…

How to delete a button that is made by a loop

from tkinter import *class Main:def __init__(self, root):for i in range(0, 9):for k in range(0, 9):Button(root, text=" ").grid(row=i, column=k)root.mainloop()root = Tk()x = Main(root)How do I…

Invalid array shape with neural network using Keras?

Currently studying the Deep Learning with Python book by Francios Chollet. I am very new to this and I am getting this error code despite following his code verbatim. Can anyone interpret the error mes…

How to download PDF files from a list of URLs in Python?

I have a big list of links to PDF files that I need to download (500+) and I was trying to make a program to download them all because I dont want to manually do them. This is what I have and when I tr…

Training on GPU much slower than on CPU - why and how to speed it up?

I am training a Convolutional Neural Network using Google Colabs CPU and GPU. This is the architecture of the network: Model: "sequential" ____________________________________________________…