How I can make apt-get install to my virtualenv?

こ雲淡風輕ζ 提交于 2019-12-02 20:33:33

If you really need to do it this way, you can just copy the files that get installed globally directly into your virtualenv. For example I couldn't get pycurl working since the required libraries weren't installing, but apt-get install python-pycurl did. So I did the following:

sudo apt-get install python-pycurl
cp /usr/lib/python2.7/dist-packages/pycurl* ~/.virtualenvs/myenv/lib/python2.7/site-packages/

The install said it was adding it to /usr/lib/python2.7. So I looked in that directory for a site-packages or dist-packages with pycurl, after looking at the files I copied them into my virtualenv. You'd have to also copy any executables from bin into your virtualenv's bin directory.

Also, running a pip install -r requirements.txt successfully found pycurl in there and just skipped over it as if I had installed it via pip.

Why would you want to do this? The whole point is to avoid doing stuff like that...

virtualenv whatever
cd whatever
bin/pip install dbus-python

You may also choose to specify --no-site-packages to virtualenv to keep it extra isolated.

First install the dbus development libraries (you may need some other dev libraries, but this is all I needed)

sudo apt-get install libdbus-1-dev libdbus-glib-1-dev

Next, with your virtualenv activated, run the following. It'll fail but that's ok.

pip install dbus-python

Finally, go into your virtualenv's build directory and install it the non-pythonic way.

cd $VIRTUAL_ENV/build/dbus-python
chmod +x configure
./configure --prefix=$VIRTUAL_ENV
make
make install

An alternative solution is to install globally, then followed by allowing the virtualenv to be able to see it. As an example, let's say we want to install matplotlib for Python 3:

  1. sudo apt update # Update first
  2. sudo apt install python3-matplotlib # Install globally
  3. sudo pip3 install -U virtualenv # Install virtualenv for Python 3 using pip3
  4. virtualenv --system-site-packages -p python3 ./venv #the system-site-packages option allows venv to see all global packages including matplotlib
  5. source ./venv/bin/activate #activate the venv to use matplotlib within the virtualenv
  6. deactivate # don't exit until you're done using the virtualenv
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!