Calling IPython from a virtualenv

前端 未结 8 2005
既然无缘
既然无缘 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:08

    You can force IPython to use a virtual environment if available by adding file below to ~/.ipython/profile_default/startups:

    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
        sys.path = filter(lambda p: not p.startswith(dist_site), sys.path)
    
        # add virtualenv site
        sys.path.insert(0, virtual_site)
    

    I recommend naming it 00-virtualenv.py so changes will be made as early as possible.

    Note: Make sure ipython is installed in the new virtual environment to get this to work.

提交回复
热议问题