Setting up a Virtual Environment and GitHub Repo : A Step-by-Step Guide

Nelsonjoseph
9 min readNov 16, 2023
Source : Internet

Setting up a virtual environment for Python projects is indeed a crucial step in software development. It allows for the isolation of project dependencies and ensures consistency across different environments. Creating a Python virtual environment in Windows involves a series of steps to ensure a smooth and error-free setup. Additionally, integrating the local and remote repositories on GitHub is essential for version control and collaboration.

While the steps you’ve outlined are valuable, it’s important to note that the process may vary based on the specific project requirements and the tools being used. It’s also crucial to emphasize the significance of documenting the setup process for future reference and sharing with other team members.

It would be daunting to read if I am explaining the different ways we can create virtual environments and how I finally ended up using this way. So let me get to the point straight.

Pre requisites:-

  • Basic Knowledge about why we are creating a Virtual Environment.
  • Basic knowledge of Github
  • Basic knowledge of Git Bash
  • VS Code ( IDE )
  • Python
  • Command line Basic Commands

Let’s start

There are two approaches for setting up a Virtual Environment(VE):-

  1. When you have mulitple projects that requires almost similar dependencies and you will be working in the same environment for all of them.
  2. If you want to create individual virtual environment for each of your projects and create their remote Github Repository.

This blog will only focus on the first approach. I will try to make this simple as possible. The first thing we need to do is to install the python library ‘virtualenv’. This is the first step towards creating a virtual environment for your project. Virtual Environment will be represented as VE throughout the blog.

pip install virtualenv

Now the library is installed. we have to navigate through the command line to the folder where we want to make the Virtual Environment(VE).

Let’s say I wish to create my VE in the desktop. When you open your command line the first time it will be looking something like this.

Now we have to navigate to desktop so we use the ‘cd’ command.

cd desktop

Now to create a Virtual Environment(VE) use the below command.

virtualenv venv

Here, ‘venv’ is the VE name and can be replaced with any name. If you are planning to create an VE for Machine learning projects and you wish to work on different projects within it.

virtualenv ML

This results in the creation of a folder named ML on our desktop with several files within it. Now we have to navigate to the VE we created.

Note: It’s not activate yet. I will show you how you see when it is activated.

Let’s check the list of libraries within our ML(VE) folder using the below command.

pip list

Now we are seeing a lot of libraries over here. It will be different for you and a huge number of libraries will be there too.

To activate the Environment we created we have to run the activate file within the Scripts folder within the Environment folder we created ie, ML folder in this case. Run the below command.

Scripts\activate

Now the environment is activated. You can see the environment name on the left side of the command line.

Let’s check the list of the libraries present within the activated virtual environment.

Here, we can see there are only three libraries when the environment is activated. These three packages provide necessary tools to easily manage and install Python packages within that environment.

Let’s try installing some packages and check if it reflects in our environment. I am going to install numpy and tensorflow, use the below command.

pip install numpy tensorflow

Now, we can see that numpy and tensorflow is installed along with all the dependencies that is required by these libraries is installed. Now if your project require any other libraries use ‘pip’ to install them when the Environment is in an activated state otherwise it will not reflect specific to the Environment.

Now we can start on a project. Let’s say I am planning to create a project on “Stock Price Prediction using LSTM”. So let’s create a Github repository for this project.

Type the desired name for the repository in the Repositoy Name and create the repository.

Now, inorder to make the local copy of the remote repository in Github. We have to navigate back to the ML (Environment folder).

First, you have to deactivate the Environment. Use the below command.

deactivate

Now to make the local copy we have to use the below command.

git clone https://github.com/nelson123-lab/Stock-price-prediction-using-LSTM.git

The above line of code clones my Github Repository. If you are unaware about where to get the repository hyperlink, check the below screenshot. If you click on the ‘code’ button you can see the Github repository hyperlink

Now, we can see a local copy of Stock-price-prediction-using-LSTM Github Repository is created within our ML(Environment) folder.

Now open the folder Stock-price-prediction-using-LSTM in vs code editor.

When you open this folder it will look like this.

Now, if you make any changes to the local copy it will show in the commit area, these are the contents that are not present in the remote repository in Github Repository.

If you click on the source control option( 3rd icon )on the slidebar you have option to commit the file to the remote repository.

When you click on the commit you have the option to type in the commit message, “modelfile” as in this case shown below. The commit message can be anything depending on what you what it to be.

Now you have the option to confirm the commit you did by pressing the confirm button on the right side.

When you are done with the commit you will see the option to push your changes from local copy to remote repository in Github by clicking “sync changes”.

When you click sync changes it will push your code from local copy to remote repository.

We can see the Model.py file is added over here.

Since we are done with the cloning Github repository to local now we add our project and push the neccessary changes to github.

Now we have the Environment and the local copy of the project within the Virtual Environment let’s try to write some code within the Model.py file.

But it’s showing the ModuleNotFoundError what might be the reason ?

Think about it. We have already installed tensorflow earlier within the Environment after it was created.

You might think it’s because the Python Environment is not activated. Even if you activate the virtual environment it can still show that the module is not found. Why you did all these works if you can’t get your desired packages installed.

The issue here is that you are not opening the right folder in the VS code. When you load a folder within the Virtual Environment folder you won’t be able to change your environment that you are working on, you won’t be having the option to choose the right interpreter for your virtual environment. You have to choose the Python Environment folder. Let’s see examples of both the cases.

When you are opening the project folder within the Environment and check for the active interpreter you won’t be able to see the activated Virtual Environment.

Use Ctrl+Shift+P to choose your interpreter in VS Code.

In the above figure you are not seeing any VE associated with our project. But if you are loading the VE folder directly, you can see there will an active VE that you can choose from the python interpreter menu as shown below.

After selecting the Python 3.11.6(‘ML’: venv) you won’t be seeing any errors for the dependencies.

Now you can make your project and push your updates to the github. We usually don’t add the VE files that includes the Lib, Scripts, Include within the Github Repository, instead add requirements.txt inside the Github Repository. In this case we don’t need to add the VE files in the .gitignore since these will not be tracked by the Github Repository.

You can use the below command when you have the VE in an activated state to get the requirements.txt.

pip freeze > requirements.txt

Now we can see a requirements.txt file in our ML Environment folder.

We have to move it to the projects folder and push it to the project repository. This allows anyone to try out your work just by installing all the dependencies from the requirements.txt.

Now you are ready with the project setup to work further. If you are installing newer packages in future get the latest requirements.txt and push it into the Github Repository.

Now anyone forking the repository can get all the dependencies by running the following command. This will install all the packages neccessary for your project that were present in the Virtual Environment.

pip install -r requirements.txt

Conclusion

This is the case when you have multiple projects that requires similar dependencies and you want to create a Virtual Environment in general for them. There is an issue in using such a case. Suppose, you were working with a deep learning project that requires pytorch initially and currently you are working on a machine learning project that uses only sklearn, numpy and scipy. The requirements.txt will be having pytorch as well since the virtual environment have it and the project is not using it.

This will install all the unnecessary libraries mentioned in the requirements.txt along with the required libraries. We will explore creating GitHub repositories and virtual environments customized for individual projects in the next blog.

Note:- All commands used in this blog are present in the Github Repository

Before You Go

Thanks for reading! If you want to get in touch with me, feel free to reach me at nelsonjoseph123@gmail.com or my LinkedIn Profile. You can view my Github profile for different data science projects.

Buy me a coffee

--

--