diff --git a/README.rst b/README.rst index 45dce6a..b767a34 100644 --- a/README.rst +++ b/README.rst @@ -25,7 +25,7 @@ Installation pip install pipreqs -Obs.: if you don't want support for jupyter notebooks, you can install pipreqs without the dependencies that give support to it. +Obs.: if you don't want support for jupyter notebooks, you can install pipreqs without the dependencies that give support to it. To do so, run: .. code-block:: sh @@ -67,6 +67,7 @@ Usage | e.g. Flask>=1.1.2 | e.g. Flask --scan-notebooks Look for imports in jupyter notebook files. + --venv ... Look for imports in the specified virtualenv Example ------- diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index dceda96..3a46309 100644 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -37,6 +37,7 @@ Options: | e.g. Flask>=1.1.2 | e.g. Flask --scan-notebooks Look for imports in jupyter notebook files. + --venv ... Look for imports in the specified virtualenv """ from contextlib import contextmanager import os @@ -109,6 +110,7 @@ def get_all_imports(path, encoding="utf-8", extra_ignore_dirs=None, follow_links ".tox", "__pycache__", "env", + ".env", "venv", ".venv", ".ipynb_checkpoints", @@ -260,10 +262,10 @@ def get_imports_info(imports, pypi_server="https://pypi.python.org/pypi/", proxy return result -def get_locally_installed_packages(encoding="utf-8"): +def get_locally_installed_packages(encoding="utf-8", paths: list = sys.path): packages = [] ignore = ["tests", "_tests", "egg", "EGG", "info"] - for path in sys.path: + for path in paths: for root, dirs, files in os.walk(path): for item in files: if "top_level" in item: @@ -301,8 +303,8 @@ def get_locally_installed_packages(encoding="utf-8"): return packages -def get_import_local(imports, encoding="utf-8"): - local = get_locally_installed_packages() +def get_import_local(imports, encoding="utf-8", paths: list = sys.path): + local = get_locally_installed_packages(encoding=encoding, paths=paths) result = [] for item in imports: # search through local packages @@ -504,6 +506,7 @@ def init(args): global scan_noteboooks encoding = args.get("--encoding") extra_ignore_dirs = args.get("--ignore") + venv_dirs = args.get("--venv") follow_links = not args.get("--no-follow-links") ignore_errors = args.get("--ignore-errors") @@ -520,6 +523,9 @@ def init(args): if extra_ignore_dirs: extra_ignore_dirs = extra_ignore_dirs.split(",") + if venv_dirs: + venv_dirs = venv_dirs.split(",") + path = ( args["--savepath"] if args["--savepath"] else os.path.join(input_path, "requirements.txt") ) @@ -554,7 +560,10 @@ def init(args): imports = get_import_local(candidates, encoding=encoding) else: logging.debug("Getting packages information from Local/PyPI") - local = get_import_local(candidates, encoding=encoding) + if venv_dirs: + local = get_import_local(candidates, encoding=encoding, paths=venv_dirs) + else: + local = get_import_local(candidates, encoding=encoding) # check if candidate name is found in # the list of exported modules, installed locally