What is a virtual environment and how to use pyenv?

What is a virtual environment and how to use pyenv?

Play this article

Hello from my very first article. I would like to talk about what is a virtual environment and how to use it?

First of all, a virtual environment can be assumed as a copy of your python environment. Yes, the one you run all your codes, import the libraries, etc.

Secondly, why do we need this virtual environment thing? Can't we already have one python environment? YES, you have but if you accidentally update one of the packages you used and that package triggered the another to be updated and months later you may not be able to run your old codes because of the version chaos. The solution comes with a virtual environment, of course. You basically create one and when you activate the virtual environment, you will be able to use that as a python environment and anything you do in it won't affect the real python environment of yours.

Let's cut short and show the code..

  1. Installation

    a. Pyenv can be installed easily on MacOs but on the other OSs, it could take time. you should visit the link.

    brew update
    brew install pyenv

    b. Pyenv-virtualenv is the same, unfortunately. If you use any other OS different than MacOs, it is probably best for you to visit the link.

    brew install pyenv-virtualenv
  2. How to use? Believe me, pyenv will save you so much time. First, you install the pyenv, then you will be able to use install any python version you want and as many as you want.

    pyenv install 3.8.6
    pyenv install 3.8.5
    pyenv install 3.8.4

    Okay, we installed these versions but what now? Now is the time we assign these versions. Yes, assign. You may be sick of changing versions in every folder you stepped in. Pyenv does this for you. Let me demonstrate. Initially, we need to see how many versions we have and which versions we installed?

    pyenv versions

    Moreover, we assign one of them as a global version. As you can understand, global means can be used in any folder except locally assigned.

    pyenv global 3.8.6

    Now if we write python into the terminal, then python version 3.8.6 will pop up. Let's assign local now.

    mkdir haha
    cd haha
    pyenv local 3.8.5

    If you try to write python into terminal while you are in the haha directory, then you'd face python with version 3.8.5 and anything you install by using pip will be stick to that version. This means you won't be able to use the package you install in haha directory unless you assign python 3.8.5 in any other directory.

    cd haha
    pip install numpy
    >>> import numpy
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'numpy'

    But if we want to use packages with different versions in the same python version. That is easier. We create a pyenv virtual environment. Let's see.

    pyenv virtualenv 3.8.6 virtualenv_name

    Easy peasy lemon squeezy. The coolest part is, usage is the same!

    cd haha
    pyenv local virtualenv_name
    pip install tqdm

    Keep coding!

Did you find this article valuable?

Support Kaan Berke UGURLAR by becoming a sponsor. Any amount is appreciated!