0

I want to detect imported packages in one python module. But this will be done on another python module.

I couldn't find a way to do apart from opening the module I want to get imported package names and find the "import" string, use split with space and get the name of the package.

with open(path, 'r') as file:
    for line in file:
        if "import" in line:
            seperate = line.split(" ")
                if seperate[0] == "import":
                    print(seperate[1].rstrip())

Is there any advice?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • If you're open to an external solution, [pipreqs](https://github.com/bndr/pipreqs) seems to do what you are trying to do. They use a regex + ast based method FWIW. – kennyvh Apr 24 '21 at 22:17
  • 1
    Emre, if you found a solution to your problem, do post an answer explaining it. Don't edit the question with stuff like `SOLVED` – Tomerikoo Apr 24 '21 at 22:20
  • @Tomerikoo is it ok now? I am new at here :) – iPatavatsizz Apr 24 '21 at 22:30
  • You should have marked that link as the duplicate if you found your answer there, but now it is OK. It is added to the list of links at the top of the page so others will see it if they get here – Tomerikoo Apr 24 '21 at 23:32

0 Answers0