Python package installed globally, but not in a virtualenv (PyGTK)

前端 未结 5 1010
礼貌的吻别
礼貌的吻别 2021-01-01 05:23

I\'m having some strange issues with PyGTK in \"virtualenv\". gtk does not import in my virtualenv, while it does import in my global python install. (I wasn\'t having this

相关标签:
5条回答
  • 2021-01-01 05:44

    It is now possible to resolve this using vext. Vext allows you to install packages in a virtualenv that individually access your system packages. To access PyGTK, do the following:

    pip install vext
    pip install vext.pygtk
    
    0 讨论(0)
  • 2021-01-01 05:45

    Another way to do this is to create a .pth file in your virtualenv's site-packages dir

    eg

    (in <virtualenv>/lib/python2.7/site-packages/dist-packages.pth)

    /usr/lib/python2.7/dist-packages/
    

    This fixed the issue I was having with apt-get installed version of pycairo

    0 讨论(0)
  • 2021-01-01 06:04

    So gtk normally lives in a place like /usr/lib/python2.7/dist-packages which is in your Python path in your global environment, but not in your virtual environment.

    You may wish to just add the path to gtk manually with something like

    import sys
    sys.path.append("/usr/lib/python2.7/dist-packages/gtk")
    

    You could also change the path when you activate the virtual environment. Open up venv/bin/activate. Its a scary looking file, but at the end you can just put:

    export PATH=$PATH:/my/custom/path
    

    Save that and the next time you activate the virtual environment with:

    source venv/bin/activate
    

    your custom path will be in the path. You can verify this with

    echo $PATH
    

    An alternative approach suggested Python: virtualenv - gtk-2.0 is to go into your virtualenv directory and add a 'dist-packages' directory and create symbolic links to the gtk package you were using previously:

    mkdir -p venv/lib/python2.7/dist-packages/
    cd venv/lib/python2.7/dist-packages/
    

    For GTK2:

    ln -s /usr/lib/python2.7/dist-packages/glib/ glib
    ln -s /usr/lib/python2.7/dist-packages/gobject/ gobject
    ln -s /usr/lib/python2.7/dist-packages/gtk-2.0* gtk-2.0
    ln -s /usr/lib/python2.7/dist-packages/pygtk.pth pygtk.pth
    ln -s /usr/lib/python2.7/dist-packages/cairo cairo
    

    For GTK3:

    ln -s /usr/lib/python2.7/dist-packages/gi gi
    

    Full disclosure: I feel that both these solutions are somewhat hackish, which is ok given that you say the question is urgent. There is probably a 'proper' way to extend a virtual environment so let us know if you eventually discover the better solution. You may have some luck with http://www.virtualenv.org/en/latest/index.html#creating-your-own-bootstrap-scripts

    0 讨论(0)
  • 2021-01-01 06:07

    If you want to include the links to the relevant system's python gtk-2.0 in the virtualenv, you can just use pip to install ruamel.venvgtk:

    pip install ruamel.venvgtk
    

    You don't have import anything, the links are setup during installation.

    This is especially handy if you are using tox, in that case you only need to include the dependency (for tox):

    deps:
        pytest
        ruamel.venvgtk
    

    and a newly setup python2.7 environment will have the relevant links included before the tests are run.

    0 讨论(0)
  • 2021-01-01 06:08

    Try creating your virtual environment with the --system-site-packages flag.

    0 讨论(0)
提交回复
热议问题