0

I have a project that I want to work in a different Python version from my OS. In this example my OS is Ubuntu 22 - Python 3.10.4 and I want to create a project using Python 3.8.4.

When we want to work with a new python environment with different libraries from our OS system we usually create new environment through virtualenv for example. Whit this approach we will have the env folder inside our main project and we can commit it to our github repository. Once one clone it, just activate the environment and (env)$ pip3 install -r ./requirements.txt in order to load all python dependencies for that project.

The problem using virtualenv is that it creates a copy from my OS python version when building a virtual environment. The point here is that, as said before, I have python 3.10.4 and I need 3.8.4 in my env.

In order to achieve this I installed the pyenv. It works like a NVM to nodejs and I can easily switch between python versions in my OS without mess or affect my main OS.

Following the instructions in the documentation I managed to install the version 3.8.4 in my system. In fact it installs the desired versions inside ~/.pyenv/versions and then from there we can switch versions.

$ pyenv versions
system
3.8.4
3.8.4/envs/test3.8
* test3.8 (set by /home/user/Documents/vhosts/py-whatsapp-message-response-automation/.python-version)

In order to create a virtual environment env folder inside my repository would be enough to switch the python version in my operational system through pyenv approach and then follow the instructions to create the virtual environment using python3 -m venv env. I did not test this option.

Instead, I followed the steps Using pyenv to Install Python in the documentation. It works and creates kind of link between my Project Repository and the Python version inside ~/.pyenv/versions. From here I can activate the environment and install the desired libraries.

The main problem here is that, if another user clones the repository it will not have the env folder in order to install the libraries to the project through (env)$ pip3 install -r ./requirements.txt. All the information about this repository are located out of the repository at ~/.pyenv/versions and it can't be commited.

I would like to know how to create the env folder or environment folder inside the project using pyenv approach in a way that I could make it available to be commited in the version control.

IgorAlves
  • 5,086
  • 10
  • 52
  • 83
  • 1
    Since you have gone through the steps at [RealPython](https://realpython.com/) for [virtual-environments-and-pyenv](https://realpython.com/intro-to-pyenv/#virtual-environments-and-pyenv) you might like to next read through [pipenv-guide](https://realpython.com/pipenv-guide/). In general, your other users (developers, team members, interested followers) are responsible to create the environments but you can provide what they need in terms of a pipfile or a pipfile.lock to ensure they know how to set up their environment (also targeting either development or production environments as well) – topsail Jul 04 '22 at 20:22
  • 1
    after read your suggestion I applied some tests. It seems that the best option, really straightforward is to do what did not do. Change my global python to the desired version `pyenv global 3.8.4`, create my repository and environment to the version 3.8.4 `python3 -m venv env` and finally set back the global python to the previous version `pyenv global system`. After this process I have a repository with `3.8.4` and a system in the original version `3.10.4` – IgorAlves Jul 06 '22 at 01:01

0 Answers0