Hey everyone, this is just a quick tutorial on how you can generate requirements.text file for your Python project. If you haven't been keeping track of all your imports and you haven't been maintaining a virtual environment.
So I started off this project. It's got quite a few imports. Some of these are built in modules, but you can see there's a couple of ones like Selenium and Rich.
And in this test file, I also have some other modules and I don't have a requirements.text file and I haven't generated a virtual environment.
So if I were to manually create a requirements.text file, I would have to manually do that. But I'm pretty lazy, so I don't want to do that. The best way that I've seen to do this is using a module called Pipreqs.
And Pipreqs, what it does is it has a look at your project. It has a look at all your imports, then it automatically generates the requirements.text for you. So I'm going to go ahead and show you how easy it is to get started with that.
First thing I'm going to do is create a virtual environment. You don't need to do this. I'm just doing it so I can show off the install of Pipreqs. So to create the virtual environment,
pip -m pip install venv
And this generates my Python virtual environment. Basically, this is just a Python environment that doesn't have any Python modules installed in it.
source VIRTUAL_ENV_NAME/bin/activate
So to activate that virtual environment, I go vm scripts and then activate.
Okay, so now let's get started with Pipreqs. And to install Pipreqs, it's just a pip install Pipreqs.
pip install pipreqs
And once you have pipreqs
installed, all you need to type is pipreqs
.
# generate requirements.txt
pipreqs
And it's going to automatically create your requirements.text
file. So there it is right there. Click it and you can see that it automatically detected all the modules that I'm using in my project.
Now in regards to the version, I'm not actually sure how it determines which version it should be using. I think it just puts in the latest version unless you have the module already installed on your system.
Then it probably looks at the one that's installed locally and determines it that way. I'm not sure. But if you know, let us know in the comments below.
And just looking at the Pipreqs documentation, you can see it's basically everything I went over. There's a couple other options here available to you.
But I mostly just use this for quickly installing the requirements.text file. It's really easy and convenient. And I find myself using it on quite a few projects nowadays.
Top comments (0)