Calling IPython from a virtualenv

前端 未结 8 1985
既然无缘
既然无缘 2020-12-12 13:34

I understand that IPython is not virtualenv-aware and that the most logical solution to this is to install ipython in each virtualenv seperately using

pip i         


        
相关标签:
8条回答
  • 2020-12-12 14:13

    (Debian/Ubuntu) assuming some version (x) of Python3 is installed, then:

    $ sudo apt-get install -y ipython
    $ virtualenv --python=python3.x .venv
    $ source .venv/bin/activate
    $ pip3 install ipython
    $ ipython3
    

    will launch ipython running your version of Python3.

    0 讨论(0)
  • 2020-12-12 14:14

    If you're trying to open a notebook, even ipython 5 won't help - ipython will disregard the virtualenv (at least on my machine/setup). You'll need to use rgtk's script, but please make sure to modify the optional filter part and the sys.path.insert as below:

    import os
    import sys
    
    if 'VIRTUAL_ENV' in os.environ:
        py_version = sys.version_info[:2] # formatted as X.Y 
        py_infix = os.path.join('lib', ('python%d.%d' % py_version))
        virtual_site = os.path.join(os.environ.get('VIRTUAL_ENV'), py_infix, 'site-packages')
        dist_site = os.path.join('/usr', py_infix, 'dist-packages')
    
        # OPTIONAL: exclude debian-based system distributions sites
        # ADD1: sys.path must be a list
        sys.path = list(filter(lambda p: not p.startswith(dist_site), sys.path))
    
        # add virtualenv site
        # ADD2: insert(0 is wrong and breaks conformance of sys.path
        sys.path.insert(1, virtual_site)
    
    • ADD1: in the original script we get back a filter object, we would break sys.path and insert below would fail
    • ADD2: see this question and python documentation
    0 讨论(0)
提交回复
热议问题