Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ Session.vim
*~

/pipreqs/*.bak

.venv
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Usage

Options:
--use-local Use ONLY local package info instead of querying PyPI
--only-venv Use ONLY venv packages instead local system
--pypi-server <url> Use custom PyPi server
--proxy <url> Use Proxy, parameter will be passed to requests library. You can also just set the
environments parameter in your terminal:
Expand Down
4 changes: 4 additions & 0 deletions pipreqs/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pipreqs import pipreqs

if __name__ == "__main__":
pipreqs.main()
33 changes: 28 additions & 5 deletions pipreqs/pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

Options:
--use-local Use ONLY local package info instead of querying PyPI.
--only-venv Use ONLY venv packages instead local system.
--pypi-server <url> Use custom PyPi server.
--proxy <url> Use Proxy, parameter will be passed to requests
library. You can also just set the environments
Expand Down Expand Up @@ -49,6 +50,7 @@
import requests
from yarg import json2package
from yarg.exceptions import HTTPError
from typing import Optional

from pipreqs import __version__

Expand Down Expand Up @@ -260,10 +262,29 @@ 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(use_venv_packages: bool, encoding="utf-8"):
packages = []
ignore = ["tests", "_tests", "egg", "EGG", "info"]
for path in sys.path:

venv_path = os.environ.get("VIRTUAL_ENV")
paths_to_search = []
if use_venv_packages and venv_path:
lib_path = os.path.join(venv_path, "lib")
if os.path.isdir(lib_path):
for entry in os.listdir(lib_path):
site_packages = os.path.join(lib_path, entry, "site-packages")
if os.path.isdir(site_packages):
paths_to_search.append(site_packages)
break
else:
if use_venv_packages and not venv_path:
logging.warning(
"You specified to use only the virtual environment packages, "
"but no virtual environment is currently active."
)
paths_to_search = sys.path

for path in paths_to_search:
for root, dirs, files in os.walk(path):
for item in files:
if "top_level" in item:
Expand Down Expand Up @@ -301,8 +322,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, use_venv_packages: Optional[bool] = False, encoding="utf-8"):
local = get_locally_installed_packages(use_venv_packages=use_venv_packages)
result = []
for item in imports:
# search through local packages
Expand Down Expand Up @@ -551,7 +572,9 @@ def init(args):

if args["--use-local"]:
logging.debug("Getting package information ONLY from local installation.")
imports = get_import_local(candidates, encoding=encoding)
print(candidates)
imports = get_import_local(candidates, args["--only-venv"], encoding=encoding)
print(imports)
else:
logging.debug("Getting packages information from Local/PyPI")
local = get_import_local(candidates, encoding=encoding)
Expand Down
13 changes: 13 additions & 0 deletions tests/_data_ignore/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
asposestorage==1.0.2
beautifulsoup4==4.13.3
boto==2.49.0
docopt==0.6.2
Flask==3.1.0
ipython==8.12.3
nose==1.3.7
peewee==3.17.9
pyflakes==3.1.0
pyflakes==3.3.2
Requests==2.32.3
SQLAlchemy==2.0.40
ujson==5.10.0
1 change: 1 addition & 0 deletions tests/_data_notebook/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

17 changes: 17 additions & 0 deletions tests/test_pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,22 @@ def test_get_use_local_only(self):
for item in imports_with_info:
self.assertTrue(item["name"].lower() in self.local)

@patch("pipreqs.pipreqs.get_locally_installed_packages")
def test_get_import_local_only_from_venv(self, mock_get_local_packages):
mock_get_local_packages.return_value = [
{"name": "requests", "version": "2.31.0", "exports": ["requests"]},
{"name": "docopt", "version": "0.6.2", "exports": ["docopt"]},
]

imports = {"requests": "2.31.0", "docopt": "0.6.2", "flask": "3.0.2"}

result = pipreqs.get_import_local(imports, use_venv_packages=True)

self.assertEqual(result, [
{"name": "requests", "version": "2.31.0", "exports": ["requests"]},
{"name": "docopt", "version": "0.6.2", "exports": ["docopt"]},
])

def test_init(self):
"""
Test that all modules we will test upon are in requirements file
Expand Down Expand Up @@ -194,6 +210,7 @@ def test_init_local_only(self):
"--savepath": None,
"--print": False,
"--use-local": True,
"--only-venv": False,
"--force": True,
"--proxy": None,
"--pypi-server": None,
Expand Down