Search code examples
pythonpathvirtual-environment

virtual environment approach for packages


I'm starting to use virtual environments. I'm a little unclear on how to efficiently work with them and globally installed packages. For example, if I want to use Numpy in my project within a virtual environment, does it make sense to install Numpy into the environment for the project? I'm assuming no, since I will then have to install Numpy many times for different projects. So maybe I need to set environment $PYTHONPATH for each project? Is it correct that there should be a separate $PYTHONPATH variable for each project? Or is there some other way to manage this correctly? Thanks for any recommendations you may have to offer.


Solution

  • I have personally adopted the habit of making a virtual environment (venv) for each significant project, plus a default venv in my home directory. This might help you also. Python venvs are cheap!

    I name each venv the same as my python project.

    Bonus tip: Imagine a project called blah, then there would also be a venv at ~/venvs/blah. A bash alias could be defined called blah that activates the venv and cds into the project directory:

    alias blah="source ~/.venv/blah/bin/activate; cd ~/work/blah"
    

    Now you type blah at a shell prompt and you are working on that project. Boom!