簡體   English   中英

無法在 EC2 上的 virtualenv 中 pip 安裝包

[英]Cannot pip install package in virtualenv on EC2

我在 ec2 上看到了這個奇怪的問題。 我正在嘗試在我的 virtualenv 中安裝lsm-db包,它說它已成功安裝,但是在嘗試導入包或執行pip list它不存在。

我創建了這個 virtualenv

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

然后運行which pythonwhich pipvenv顯示正確的<path-to-my-project>/venv/bin/pip 然后我運行pip install -r requirements.txt其中包含sqlalchemylsm-db 顯示警告

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, click
  Running setup.py install for sqlalchemy ... done
  Running 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

盡管有警告,最后一行顯示它們已正確安裝。 然而,他們不是。 運行 pip install lsm-db 給出

▶ 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-db
  Downloading 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-db
  WARNING: Building wheel for lsm-db failed: [Errno 13] Permission denied: '/home/ec2-user/.cache/pip'
Failed to build lsm-db
Installing collected packages: lsm-db
  Running setup.py install for lsm-db ... done
Successfully installed lsm-db-0.6.4

它再次顯示它已安裝,但事實並非如此。 不在 pip 列表中,無法在 Python 中導入。 這里發生了什么? 為什么我在virtualenv看到權限問題? 如果這是一個潛在的問題,我沒有使用 sudo 創建virtualenv 是否與我安裝virtualenv的方式有關?


編輯

我更改了權限並使用sudo yum install python36-pip重新sudo yum install python36-pip 現在我看到了這個......抱歉,這似乎是一個新問題

Running setup.py install for lsm-db ... error
    ERROR: 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 install
    running build
    running build_ext
    building 'lsm' extension
    creating build
    creating build/temp.linux-x86_64-3.6
    creating build/temp.linux-x86_64-3.6/src
    gcc -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.o
    lsm.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/

編輯

謝謝指點,現在我在ec2上安裝了python開發包,再次嘗試安裝lsm-db

> sudo yum install python36-devel
...
> pip install lsm-db
Collecting lsm-db
  Using cached https://files.pythonhosted.org/packages/17/f2/dc68ecdece424eb206c42b8db924c5a7f6dd97d26df42a1c9fb41a6fe555/lsm-db-0.6.4.tar.gz
Building wheels for collected packages: lsm-db
  Building wheel for lsm-db (setup.py) ... done
  Stored 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

再次,它表明它是成功的, which python python 在我的 venv 中為我提供了正確的 python。 但是,我的結果與開頭完全相同, pip list沒有lsm-db ,我無法在 python 中導入它。

這完全是 ec2 的問題,我可以在我的 mac 上的 virtualenv 中成功使用lsm-db 這里ec2上的virtualenv肯定有問題,為什么venv中正確的pip不能在venv中安裝包?? 我完全迷失了。 有經驗的朋友請指教!

(為了解決這個問題,我在我的代碼中從sqlitedict lsm-db切換到sqlitedict 。安裝sqlitedict沒有問題,所以它可以將懷疑范圍縮小到lsm-db特定的問題。)

2016 年的這個問題與我的更相似,但沒有結論為什么或任何解決方案。 Pip 說 pymongo 已安裝,但未將其顯示為已安裝

另一個可能相關的問題: Amazon EC2 virtualenv: pip 說它安裝了 numpy 但 python 找不到它

問題似乎與系統文件權限有關。 您可以使用以下命令從終端檢查文件夾“/home/ec2-user/.cache/pip”的其他用戶的 owner:group 以及rwx權限:

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

要更改文件夾的所有者,請使用命令:

須藤 chown -R ec2-user /home/ec2-user/.cache/pip

另一種解決方法是通過以下方式允許所有用戶擁有 rwx 權限:

須藤 chmod -R 777 /home/ec2-user/.cache/pip

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM