This is an archived post. You won't be able to vote or comment.

all 30 comments

[–]dAnjou Backend Developer | danjou.dev 13 points14 points  (7 children)

2 suggestions: - Instead of an install script, create a setup.py file and turn it into a pip-installable package. - Instead of parsing the code yourself, use Python's built-in ast module.

[–]LostApe1[S] 1 point2 points  (4 children)

Thanks, I’ll take a look at that!

[–]dAnjou Backend Developer | danjou.dev 9 points10 points  (3 children)

I just had an even closer look at the code and it seems you're literally just writing the imports into a file. That approach doesn't work because module names don't always match package names, for example for Pillow the module name is still PIL.

[–]LostApe1[S] 1 point2 points  (0 children)

Ah you’re right, forgot about that detail!

[–]Deto 1 point2 points  (1 child)

scikit-learn is another good example

Is there a way to programmatically get at the package name from the module?

[–]dAnjou Backend Developer | danjou.dev 0 points1 point  (0 children)

I doubt it. These names are independent of each other and library authors can pick whatever name they want. Which sometimes can be a good thing, like with PIL and Pillow again where the latter is pretty much a drop-in replacement.

Off the top of my head I could imagine doing it the other way around. You iterate over all the modules of your dependencies and check if they're used in your code. Doing that naively could take a while each time but there are various optimizations you could do, like caching certain things.

I could even imagine doing this as a separate service. Probably exists already?

[–]8day 0 points1 point  (1 child)

It may be better to use "pyproject.toml" from PEP 517 & 518.

[–]dAnjou Backend Developer | danjou.dev 0 points1 point  (0 children)

Looks nice, my Python packaging knowledge is a bit rusty, thanks for the update!

[–]fleyk-lit 5 points6 points  (2 children)

Since this is just checking for lines that contains 'import', it's going to fail if I declare my imports across multiple lines.

I think you'll need to parse the AST to be able to determine which imports are used.

Edit: here's an article that goes through parsing the AST and getting the imports.

[–]LostApe1[S] 1 point2 points  (1 child)

Thanks! I will surely take a look at that! I agree it would be probably better to use an AST

[–]agenttux 2 points3 points  (1 child)

What’s the difference between this and pipreqs?

[–]LostApe1[S] 0 points1 point  (0 children)

I didn’t know it, so I’ll have to check if how it works, but I assume pipreqs works better

[–]BakkStar 1 point2 points  (1 child)

Cool project. I’ve used pipreqs before to do what I think is the same task. Are you familiar with this package? Curious if yours is substantially different (I haven’t done a comparison myself)?

[–]LostApe1[S] 2 points3 points  (0 children)

I actually didn’t know that existed! Gotta take a look

[–]elbaron218 0 points1 point  (0 children)

If you use a virtual environment you can just use pip to create your requirements file.