Using Venv with Cursor AI

When doing rapid prototyping with Cursor AI, it is useful to keep you environments separate. Venv can help with this. But how do you get CursorAI to use the correct virtual environment?

Creating a Python Virtual Environment

First create your project folder. Then create a virtual environment inside it by navigating to the folder in a command prompt and running python -m venv .\venv. This should create the virtual environment folders that you need inside a folder called venv\ in your project. This is useful because the standard gitignore file for python projects excludes the contents of  the venv\ folder for the purposes of source control. This is needed because virtual environments refer to your specific base python install and so are not useful to others. Documentation on venv can be found here. installation instructions to install packages inside a virtual environment can be found here

Note that when you activate a virtual environment it is called by its folder name so you may wish to depart from the venv convention if you are juggling a lot of virtual environments. Remember to include a requirements.txt filein your project folder, so that people know what libraries to install if sharing code on Github.

Install Cursor AI

Next install  Cursor AI (which can be downloaded and installed from www.cursor.com). You will have to login to it, since cursor requires an account to use. Next open a project and pick the base folder for your project into which you installed the virtual environment as the folder to open. Now in the terminal window inside cursor you need to activate the virtual environment with the command .\venv\Scripts\Activate. This with prefix your command line prompt with (venv) and typing where python should return the location of your virtual environment as well as your base python install

Once you have activated your virtual environment you can pip install the libraries you need and get to work. Overall its pretty similar to the method in VSCode.

Keeping Library Requirements Minimal

OK so now we have a nice virtual environment. But how do we make sure that we keep a nice requirements.txt file in the project that does not become too crufted up with libraries which get tried but ultimately rejected,. Well here pipreqs and pip-tools come to the rescue. The pipreqs library will retrieve only the libraries which are actually used by your project, while pip-tools can be used to also add their dependencies to the requirements.txt file. The process is:


# first install your libraries and arrange your imports in your files

pip install pipreqs
pip install pip-tools

pipreqs --savepath=requirements.in && pip-compile && del requirements.in

Git and Github Setup

If you want this project folder to be a git repo you can use the git command line to intialise the requisite git files with

 
# You need a github account with an SSH Key in your profile for authentication
# create SSH 
# key https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
# Create a repository on Github 
# https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-new-repository
# Navigate into your local project directory

# Initialize git in your local project
git init.
# Link it to your github repo
git remote add origin https://github.com/<username>/<reponame>.git
# stage all changes
git add -A
# commit the changes
git commit -m "First commit message"
# push the changes to github
git push origin master

Congratulations you have a nice new virtual environment set up to work with CursorAI and Github. And you can also easily create a minimal requirements file.

Using Cursor AI

There are lots of articles out there about using CursorAI, and its pros and cons. You might find these useful:

https://brianchristner.io/how-cursor-ai-can-make-developers-10x-more-productive/

https://www.datacamp.com/tutorial/cursor-ai-code-editor

https://daily.dev/blog/cursor-ai-everything-you-should-know-about-the-new-ai-code-editor-in-one-place

One Reply to “Using Venv with Cursor AI”

  1. This is super helpful! I always struggle with setting up venv with different IDEs. The steps for Cursor AI are really clear. Gonna try this out on my next project! Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.