Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ship requirements.txt to users without development-packages such as PyLint etc.?

I'm new to python. I use Visual Studio Code.

My current understanding is as follows:

For each project, I create a virtual environment. Now if I want to have some linter while coding, I need to install it with "pip install pylint" for example. IF I want to ship my code to users now then I should create a requirements.txt. However, after installing pylint, it looks like this:

astroid==2.3.3
colorama==0.4.3
isort==4.3.21
lazy-object-proxy==1.4.3
mccabe==0.6.1
pylint==2.4.4
six==1.13.0
wrapt==1.11.2

All these packages are actually not necessary for my code, they all come from pylint.

  • How can I automatically create a requirements.txt which only include the packages relevant for my code (without pylint stuff)? What is the best/usual practice in python?
  • Or is this actually the "normal" way to include everything and I should not worry about it that much?
like image 739
Patze Avatar asked Dec 31 '19 20:12

Patze


People also ask

What is a requirements txt file in Python?

A requirements.txt file in Python lets you keep track of the modules and packages used in your projects. Simply put, a requirements.txt file is simply a .txt file that tracks all of your requirements. This makes it easier to install the required packages, whether for yourself or for other users of your code.

Why is the package name @/* instead of the package version?

Having @ file:///* after the package name instead of the package version is not good formatting for the requirements.txt file. It is an open issue identified in versions of pip such as 20.1 and 21.0.1. Again, the output requirements.txt file may have some extra packages, and you can always go into the file and manually remove them.

How to add error packages to the requirements list?

Sometimes the dependency is listed above the package it depend upon. The solution is simple: 1.pip install package_name 2.simply put the error package to the bottom of the requirements.txt 3.sometimes a particular version of the package is not be able to be installed,just install the newest version of it and update the data in requirements.txt

How do I remove a package from my requirements file?

You can always open your requirements.txt file and manually remove any extra packages. The code below shows a sample requirements.txt file gotten through pip freeze > requirements.txt One thing many people have noticed with some pip packages is that they put @ file:///* after some package name — were * as a wildcard character.


1 Answers

You should create a requirements.txt file that has exactly what you need to run the project and a dev-requirements.txt file that includes requirements.txt and your development dependencies. The easiest way I have found to manage this is to use a tool like pip-tools or poetry.

For the former, you would have a requirements.in which lists your execution dependencies unpinned. Then in your dev-requirements.in you would have:

-r requirements.txt
pylint

You can then use pip-compile to generate the requirements.txt file and the dev-requirements.txt files and pip-sync to make sure your packages installed in your environment matches what is specified in the requirements file.

like image 142
Brett Cannon Avatar answered Oct 23 '22 12:10

Brett Cannon