dark seone
dark seone

Reputation: 21

How to add a requirement.txt in my project python

In fact When I do pip freeze > requirements.txt to put all the packages that I use in my project in a requirements.txt, it puts all python packages that I have in my pc and this despite I have activated my visual environment.

In my project path I activated my venv then I did pip freeze > requirements.txt I had a requirement.txt with packages that had nothing to do with my project. I installed all my packages with pip. What did i did wrong.

Any help would be welcome

Upvotes: 2

Views: 21881

Answers (3)

Jamiu S.
Jamiu S.

Reputation: 5721

pip freeze is not a good option if you have chunk of installed packages, because pip freeze will include all unused packages and packages from different venv that has nothing to do with your current project in the requirements.txt.

You should use $ pipreqs --encoding=utf8 <project-dir> which is supposed to generate requirements.txt based on used packages in the current project. Read pipreqs doc
You first run the below pip command in terminal to install pipreqs incase you don't have it installed.

pip install pipreqs

After the installation run the following in terminal to generate your requirements.txt:

$ pipreqs --encoding=utf8 [<path>]

or

pipreqs --encoding=utf8 [<path>]

Note: Make sure you replace \ with \\ for your path in case you encounter FileNotFoundError: [Errno 2] No such file or directory:.

Example:

pipreqs --encoding=utf8 projects\working_directory

output:

FileNotFoundError: [Errno 2] No such file or directory: 'projectsworking_directory\\requirements.txt'

Solved

pipreqs --encoding=utf8 projects\\working_directory

output:

INFO: Successfully saved requirements file in projects\working_directory

Upvotes: 0

JL Peyret
JL Peyret

Reputation: 12164

OK, let's start out with the idea that you have a mixed globals + local packages venv. And you want to only freeze what's in the virtualenv.

You want to see what's in what
pip freeze  > local.global.txt

deactivate

pip freeze > global.txt

OK, so now you have 2 lists.

Let's make a BIG ASSUMPTION!

All the packages your project REALLY need are in the local venv site_packages.

This is probably wrong, but bear with me.

So let's look at what's in what by using a file differ.
diff global.txt local.global.txt

I am going to filter this a bit. > means it's local+global, < means it's global only.

> Django==3.2.14
> django-celery-results==2.4.0
> django-debug-toolbar==3.5.0
> django-redis-cache==3.0.1
> django-waffle==2.5.0
> django-webpack-loader==1.6.0

All this django stuff? It's only in my virtualenv. I want it in requirements.txt for this project.

Now, let's look at what I don't see in this diff but is in BOTH global.txt and global.local.txt

jupyter==1.0.0
jupyter-client==7.1.2
jupyter-console==6.4.0
jupyter-core==4.9.2
jupyterlab-pygments==0.1.2
jupyterlab-widgets==1.0.2

Yes, I can see jupyter from within my virtualenv. But I did not install it there, I am only seeing the global version. You don't want it.

Put anything in the requirements.txt that comes with >
# yes the 2 > are not a typo.
diff global.txt local.global.txt | grep '>' > requirements.txt 

Then you need to clean up the leading >

Sorry, these are some unix utilities being used here. Windows will have similar utilities in powershell or wsl (I think).

If worse comes to worse you will have to build your requirements.txt manually, by only keeping lines that DONT exist in global.txt

About that big assumption:

This is where you create a new venv with only this requirements.txt and you try to run your application and/or its tests.

Anything that it uses that you did not install locally in the virtualenv will cause errors.

Figuring out stuff that you had in the virtualenv because you installed it there by mistake (my jupyter packages for example, if I had put it there) is harder to identify.

Upvotes: 0

Yaseen
Yaseen

Reputation: 134

You may have inherited some global site packages when you created the venv. Likely ones you had pip installed while not in any venv. Try creating the venv using

Windows: python -m venv (venv name here) --no-site-packages

Linux: python3 -m venv (venv name here) --no-site-packages

The no site packages argument tells python to ignore all global-site packages when creating the new venv.

Upvotes: 1

Related Questions