Це відео не доступне.
Перепрошуємо.
DO NOT FORGET: 'requirements.txt' In Your Python Projects
Вставка
- Опубліковано 30 січ 2023
- In this lesson I will be showing you the benefits of providing a 'requirements.txt' file in your Python projects, and how to use it. DO NOT FORGET this simple file, it will make your code much more convenient to share!
▶ Become job-ready with Python:
www.indently.io
▶ Follow me on Instagram:
/ indentlyreels
Clear, useful and helpful: 'pip install pipreqs' for a clean, bare-essential module installation for the project, without all the other stuff. Thanks!
I've seen a lot of python tutorials over the last few years. But yours are simply the best out there!
I appreciate that, thank you :)
dude i really dont know how i came across your videos...but i am glad i did. Your videos are not the standard python videos 'how to write hello world' but they are USEFUL in the real world and explain all the things that there were never explained. People like me were just forced to pick up these habits of making a requirements,txt files....or writing a main function without knowing why. Thank you so much for these niche videos and really useful packages!
Sir, just want to say thank you that you save my life. I was a beginner on python and I don't know how to update the requirement.txt but your video help a lot.
Great video. I'm glad I'm not blindly copying and pasting commands anymore.
Yeah, it's always such a relief and simultaneously disappointment-at-yourself every time you find out something like this which saves you a solid 10 minutes each time you download something
At last someone who really put it in step by step!!! Thanks!
Something I do on import statements for any modules not in the standard library is to add a comment with the pip command line used to install it: e.g.
from Crypto import SHA256 # python -m pip install pycryptodome
from colors import turnip # python -m pip install ansicolors
and so on. Then I can recursively grep for lines matching 'import.*#' to get a list of the modules required (piped through a quick script to remove duplicates)
I should add that I write a lot of self-contained convenience scripts (that is, if your total 'project' is only a few hundred lines at most, it makes sense to have it as a single self-contained script, rather than a full blown project factored into separate files with a build system and such).
I suggest for getting rid of duplicates adding those lines to a txt file (or comment, as most IDEs mess up lines you don't want to, like deleting that which you don't need", or lag slightly). So for
import x
import y
I'd encapsulate lines with quotes, put commas between them (use ^ and $ regexes or
if ends of strings don't work for some reason), and add them to a set.
With:
"from x import y, z", you may match it with
"(?>from (\w+) )?import (\w+)(, \w+)?(, \w+)?(, \w+)?(, \w+)?" ...
and replace it with:
import $1.$2
import $1.$3
import $1.$4
import $1.$5
...
I'm not familiar with incremented captured groups in pure regex (without using an inside Python/programming language) to capture "(?>from (\w+) )import(?>,? (\w+))*(?>\s*#.*)?" where you would write, given that we found re.findall(ptr, pos_dup_import_lines):
lib = [] # whole library
mods = [] # modules in a library, tho I think this is wrong naming
for m in re.findall(ptr, pos_dup_import_lines):
lib = m.group(1)
mods = m.group(2)
for mod in mods:
import_lines = print( f"import {lib}.{mod}" )
Must repeat this for every line. So you'd get like:
"import my_lib_1.my_mod_1"
"import my_lib_1.my_mod_2"
"import my_lib_1.my_mod_3"
As we ignored last group (with the comment) from being included in the print statement (tho I think last group is unnecessary), we'd not have comment lines producing multiple _duplicate_ import statement. What I mean is:
"import my_lib_1.my_mod_1"
"import my_lib_1.my_mod_1 # my_mod_1 is needed"
are by definition two distinct strings, but for our purposes, they're practically duplicates.
use $ regex to append a comma, make a list of those strings, add to a set, add to a list again, sort it, print it.
Now, repeatedly, replace:
"from (\w+) import ((?>\w+)(?>, \w+)*)
from \1 import (\w+)"
With:
"from $1 import $2, $3"
The \1 ensures we match the exact library, and not just merge lines arbitrarily.
It seems obvious, but I hope someone finds this helpful.
Hi, thanks a lot for your video! This is exactly that I tried to find for my projects setup! Awesome! Thanks!
Qst: how do you handle different python versions? Do you need to have separate requirements.txt file for each python version that you use ?
Best video ever!!!! Thank you dude
Thanks!
Ahhh that was nice! Thanks!
I was trying to solve the library management problem with pipreqs but as you see in the video it get different versions of the packages that "pip freeze" gets. Also we notice that with a large project, pipreqs has a weird behaviour and installs libraries that does'nt even are imported, but are part of the environment. 🤔
Regarding venv, I understand it comes with python, but I've seen many manually install pyenv separately.. why, what's the difference between
1) pipx
2) venv / virtualenv
3) pyenv (not pyvenv ) with or without the virtualenv plugin (i.e. pyenv-virtualenv)
Can pip freeze and/or pipreqs ANALYZE my python file and determine which imports/versions are used? I'd like to get a requirements txt of ONLY the packages (and their versions) that are actually used in the code!
Thanks for the great video. Exactly what I needed. Few questions if I may: Often it is recommended to do sudo apt update/upgrade. Wouldn't it destroy the version management? 2) I just got the following error when running the requirements: "ERROR: No matching distribution found for gitpython>=3.1.30" . I looked at the gitpython web and they have versions with .30/31/32. Strangely, the command returns many versions, yet all of them are below .30 . Where exactly is the install command is looking at for versions?
Hey, what's your opinion about pipenv and poetry for handling package management?
Hi, what do i do if the project i'm trying to setup has conflicting requirements? I've tried pip freeze > requirements.txt and pip install -r requirements.txt --upgrade --upgrade-strategy eager, but there are a couple of conflicts still! 😢
good video, however -r does not stand for requirements but recursive.
wups, thanks for bringing that to my attention!
Great job
got an error message
The term 'pipreqs' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
At line:1 char:1
+ pipreqs --force
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (pipreqs:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Have you solve this issue
Greate
I got an error: "You cannot call a method on a null-valued expression" ???
PyCharm was able to figure out what I was trying to do though and eventually prompted me to load the libraries into the requirements.txt file without having to use the terminal, so that's good I guess
I might be wrong, but I'm almost certain the reason pip freeze lists module you aren't using is because you actually are using them. For example I know that requests is based on urllib3 as well as cv2 is based on numpy.
What about python egg?
What is the program you are using I see you always using it but I can’t find it anywhere, it’s probably something I already have I just don’t know what it’s name is, I currently just do everything manually with notepad++ and cmd 😂
PyCharm! Welcome to the future :)
@@allo5668 thank you so much 😂
Poetry. Goodbye.
I almost always watch and like your videos, but I had to skip this one because I can't read dark text on a black background. *Contrast* is what makes that that whole "print thing" work.
I don't understand where you see dark text on a black background in this video.
Hi, I got a small request. Could you do a video on explaining how to install different dependencies for python project? I am very new and a lot of time when I install the dependencies/library they conflict to each other such as "ImportError: cannot import name 'builder' from 'google.protobuf.internal'... "
Guys do yourself a favor and simply use poetry...
Simple package management
Simple build (laugh in the face of setuptools)
Simple publishing
Simple installation on venvs
Simple entry points
Simple extras
...
Nah, I'll stick to pyproject.toml and poetry. It's 2023 ffs.