Skip to content

add file input error handling#499

Open
MYusufY wants to merge 1 commit intobndr:masterfrom
MYusufY:master
Open

add file input error handling#499
MYusufY wants to merge 1 commit intobndr:masterfrom
MYusufY:master

Conversation

@MYusufY
Copy link

@MYusufY MYusufY commented Sep 1, 2025

This PR adds error handling for the case when the user provides a file instead of a directory.

Example:

Current version

pipreqs project/main.py:

Traceback (most recent call last):
  File "/usr/local/bin/pipreqs", line 7, in <module>
    sys.exit(main())
             ^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pipreqs/pipreqs.py", line 609, in main
    init(args)
  File "/usr/local/lib/python3.12/site-packages/pipreqs/pipreqs.py", line 599, in init
    generate_requirements_file(path, imports, symbol)
  File "/usr/local/lib/python3.12/site-packages/pipreqs/pipreqs.py", line 209, in generate_requirements_file
    with _open(path, "w") as out_file:
  File "/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pipreqs/pipreqs.py", line 91, in _open
    file = open(filename, mode)
           ^^^^^^^^^^^^^^^^^^^^
NotADirectoryError: [Errno 20] Not a directory: 'project/main.py/requirements.txt'

This fork

pipreqs project/main.py:
ERROR: The path 'project/main.py' is not a directory. Please provide a directory path.

Testing

This works properly in my tests.

Question

Is this section in contributing guideline optional?:

To run a subset of tests:

$ poetry run python -m unittest tests.test_pipreqs

Thanks!

Copy link
Collaborator

@alan-barzilay alan-barzilay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its been a while since I handled this codebase, but pretty sure pipreqs can be run with an individual file as a target so this would throw an error in a legitimate use case.

Also, the project is a bit abandoned but we previously had a policy of requiring tests for any changes and new features. I dont think there are new maintainers but even if there are they may not follow this policy anymore. Do with this information what you will, sorry I cant be a bigger help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants