Open In App

How to Install GIT on Python Virtualenv?

Last Updated : 13 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A Virtual Environment is a mechanism that can be compared with a separating funnel, when we are creating or using different projects which require different types of packages and modules we will create a virtual environment for each of them so that the packages/modules we install for say project A doesn’t affect project B. Virtual environment’s main job is to manage different python packages used for different python projects so that no package version or anything gets overlapped with other project and cause any failure. To know more about Virtual Environments and their necessity, click here.

Downloading and Installation of Git in Virtualenv

Users can write the commands in Command Prompt or the terminal of any IDE like PyCharm of VSCode. Here, we will be using the cmd. Follow the further steps to install git in virtualenv.

Step 1: Write the below command in cmd or any terminal of the user’s choice.

mkdir test-venv && cd test-venv

If the user uses any other terminal than cmd then break the above command into two like the below as the ‘&&’ will be unrecognized by other terminals.

mkdir test-venv && cd test-venv

command-in-terminal

Running the mkdir and cd command together

Step 2: After changing the directory to the newly created one, write the below command.

python -m venv env

Now, if the user gets any error with python change it to python3. Using the above command we have now created a Virtual Environment inside of that directory named test-venv (The user can give any name it doesn’t matter).

Step 3: Now, it is time to activate/start the virtual environment.

cd env

Scripts\activate

Remember to use uppercase S for Scripts. If the user is using any other terminal rather than cmd then use the command below.

source env/bin/activate

Cmd doesn’t understand the source so that’s why we had to first change the directory manually and then activate it.

Changing-directory-manually

output after activating the venv

As it is visible in front of the path we have an (env) which indicates that everything we do or every module we will install will only be available in that env (i.e Virtual Environment) and no other project will be affected by this.

Step 3: Now, for example, we will install a basic package here, one of the most common and required modules for Data Science is pandas, so we will install it here as it is lightweight and necessary. So run the following command.

pip install pandas

Installing-pandas

installing pandas module 

Step 4: Now write deactivate in the terminal or cmd to stop the Virtual Environment. As of now, we will initialize the repo with git. Write the below command.

deactivate

git init

Stopping-the-virtual-environment

deactivating the venv

Step 5: Now we will add the env folder into .gitignore so that the virtual environment is ignored every time during the source control.

echo ‘env’ > .gitignore

Step 6 (Optional): If the user wants that their repository will be used by others and the dependencies (modules/packages) they have used in their code should be installed before running their code on a different machine then it is better to create a requirements.txt file with all the dependencies used. Write the below command to do that.

pip freeze > requirements.txt

Now add it using git.

git add requirements.txt

Step 7: After adding it we will now commit it. Users can give some messages (it is recommended to do that) while committing some new changes,

git commit -m “Adding the requirements.txt file”

Committing-changes

committing the newly added files

Step 8 (Optional): Now if the user wants to see the changes reflected in GitHub then they must create a new repository without having any gitignore or readme files for now. Then write the following commands one by one.

git branch -M main

Now after using the above command user need to copy their remote_url from GitHub. 

copying-url

Github remote URL

Copy the highlighted url from the user’s repository. Then write the following command in terminal

git remote add origin <REMOTE_URL_COPED_FROM_GITHUB>

Now, for reassurance, the user might run the git add and the git commit commands again to re-assure that everything is being traced and added before pushing it to the branch. Write the below command:

git push -u origin main

git-command

pushing into main branch

Now users need to refresh their GitHub page and see all the files are being updated there.

refresh-page

after pushing updated github screen


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads