When I started learning Python and started using it’s various libraries and Frameworks, like most of the beginners I never cared about where I am installng those packages and what problems they are going to create in future.
After few days I got to know about the concept of creating virtual environment for each of our projects, so I started using that, but it was still incomplete. We’ll see how!
For those who don’t know what is virtual environment :
In simple terms you can understand, that we create a environment for our project and then whatever package we install is installed locally, at that project level, not globally.
Suppose we are working on Project A which requires version A of Django , we installed that globally. Now for project B which, suppose, uses Version B of Django things will get worse.
Your Project will not find the correct version it needs to operate and you will get weird errors.
Use pyenv to manage different version of Python in your machine.
Use venv to create and use virtual environment.
Let’s me tell you the steps :
pyenv install versionpyenv global version, by default it is system (don’t use that) [One Time]Now suppose we want to create a project
pyenv local version to declare which python version you want to use for that project.Previous step will create a .python-version file containing the python version as text.
python -m venv virtualenvname we create a virtual environment.source virtualenvname/bin/activate, we activate the venv.python -m pip install pkgnameHence we setup a project which uses a specific python version of our need.
Also we created a venv so that packages are installed locally, not globally hence no version conflicts in future.
pyenv install version : To install a python versionpyenv local version : To use specific python version in your projectpython -m venv venvname : To create Virtual environment.python -m pip install packagename : To install packagesTips for installation: