Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actions for creating venv in python and clone a git repo

Tags:

git

python

github

I'm relatively new in all that and I have a problem with the row of the actions. Say that you created a directory and you want a python virtual environment for some project and clone a few git repos (say, from GitHub). Then you cd in that directory and create a virtual environment using the venv module (for python3). To do so you run the following command,

     python3 -m venv my_venv

which will create in your directory a virtual environment called my_env. To activate this environment you run the following.

     source ./my_env/bin/activate

If in addition inside that directory you have a requirements.txt file you can run,

     pip3 install -r ./requirements.txt

to install your various dependencies and packages with pip3. Now this is where I'm getting confused. If you want to clone git repos where exactly you do that? In the same directory you just run git clone and creates the git repos or you need to cd in another folder. In order to let python venv pick up the cloned repos is the above enough, or venv must be installed after you have cloned the repos in your directory?

like image 220
user430191 Avatar asked Mar 17 '20 09:03

user430191


People also ask

How do I clone a github repository in Python?

We can use git module in python to clone the repository from git. Clone the repository you want to work with in local system. So in clone_from methods pass the two arguments in which first argument is url of your repository and second argument is the location of your directory where you want to cloned the repo.


1 Answers

First of all, you need to understand what is virtual environments, when you understand what it is for, the order of actions will be more clear.

Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface.

This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.

The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.

Different applications can then use different virtual environments. To resolve the earlier example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. If application B requires a library be upgraded to version 3.0, this will not affect application A’s environment.

※ Reference: 12. Virtual Environments and Packages


Generally, the following order is the most appropriated.

  1. $ git clone <Project A> # Cloning project repository
  2. $ cd <Project A> # Enter to project directory
  3. $ python3 -m venv my_venv # If not created, creating virtualenv
  4. $ source ./my_venv/bin/activate # Activating virtualenv
  5. (my_venv)$ pip3 install -r ./requirements.txt # Installing dependencies
  6. (my_venv)$ deactivate # When you want to leave virtual environment

All installed dependencies at step 5 will be unavailable after you leave virtual environment.

like image 126
Yeheshuah Avatar answered Sep 20 '22 03:09

Yeheshuah