Virtualenv on Ubuntu with no site-packages

后端 未结 5 2072
野性不改
野性不改 2020-12-13 07:33

I\'ve been using virtualenv lately while developing in python. I like the idea of a segregated development environment using the --no-site-packages option, but doing this wh

相关标签:
5条回答
  • 2020-12-13 07:53

    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.

    More detailed information on how the links are setup can be found in this answer

    0 讨论(0)
  • 2020-12-13 08:00

    I find in this situation, symlinks, or even copying specific files (packages, modules, extensions) works really well.

    It allows the program to emulate being run in the target environment, rather than changing the application to suit the development environment.

    Same deal for something like AppEngine.

    0 讨论(0)
  • 2020-12-13 08:01

    Check out the postmkvirtualenv hook script here:

    https://stackoverflow.com/a/9716100/60247

    In that case, he's using it to import PyQt and SIP after a new Virtualenv is created, but you can add the packages that you need to LIBS.

    And vote that script up because it's fantastic :)

    0 讨论(0)
  • 2020-12-13 08:07

    One way is to add the paths to your code using sys.path.

    import sys
    
    sys.path.append(somepath)
    

    Another way is to use site, which processes .pth files in addition to adding to sys.path.

    import site
    
    site.addsitedir(sitedir, known_paths=None)
    

    https://docs.python.org/library/site.html

    But you probably don't want to add this to all your related code.

    I've seen mention of sitecustomize.py being used to perform something like this, but after some testing I couldn't get it to work as might be expected.

    Here it mentions that auto-import of sitecustomize.py ended in 2.5, if your not on 2.5 try it out. (just add one of the path add methods above to the file and drop it in the directory your program is run) A work around method is mentioned in the post for users of 2.5 and up.

    http://code.activestate.com/recipes/552729/

    0 讨论(0)
  • 2020-12-13 08:09
    $ virtualenv --no-site-packages --python=/usr/bin/python2.6 myvirtualenv
    $ cd myvirtualenv
    $ source bin/activate
    $ cd lib/python2.6/
    $ ln -s /usr/lib/pymodules/python2.6/gtk-2.0/ 
    $ ln -s /usr/lib/pymodules/python2.6/pygtk.pth 
    $ ln -s /usr/lib/pymodules/python2.6/pygtk.py 
    $ ln -s /usr/lib/pymodules/python2.6/cairo/
    $ python
    >>> import pygtk
    >>> import gtk
    
    0 讨论(0)
提交回复
热议问题