Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix when calling from outside directory #309

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions pipreqs/pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ def get_imports_info(
return result


def get_locally_installed_packages(encoding=None):
def get_locally_installed_packages(search_path, encoding=None):
packages = {}
ignore = ["tests", "_tests", "egg", "EGG", "info"]
for path in sys.path:
for path in search_path:
for root, dirs, files in os.walk(path):
for item in files:
if "top_level" in item:
Expand All @@ -224,8 +224,8 @@ def get_locally_installed_packages(encoding=None):
return packages


def get_import_local(imports, encoding=None):
local = get_locally_installed_packages()
def get_import_local(search_path, imports, encoding=None):
local = get_locally_installed_packages(search_path)
result = []
for item in imports:
if item.lower() in local:
Expand Down Expand Up @@ -408,6 +408,11 @@ def init(args):
input_path = args['<path>']
if input_path is None:
input_path = os.path.abspath(os.curdir)
search_path = sys.path
else:
search_path = sys.path
search_path.remove(os.path.abspath(os.curdir))
search_path.append(os.path.abspath(input_path))

if extra_ignore_dirs:
extra_ignore_dirs = extra_ignore_dirs.split(',')
Expand All @@ -429,10 +434,10 @@ def init(args):
if args["--use-local"]:
logging.debug(
"Getting package information ONLY from local installation.")
imports = get_import_local(candidates, encoding=encoding)
imports = get_import_local(search_path, candidates, encoding=encoding)
else:
logging.debug("Getting packages information from Local/PyPI")
local = get_import_local(candidates, encoding=encoding)
local = get_import_local(search_path, candidates, encoding=encoding)
# Get packages that were not found locally
difference = [x for x in candidates
if x.lower() not in [z['name'].lower() for z in local]]
Expand Down