I want to create a script that looks inside a Python file and finds all import
statements. Possible variations of those are the following:
import os
import numpy as np
from itertools import accumulate
from collections import Counter as C
from pandas import *
By looking at these, one could argue that the logic should be:
Get me all <foo>
from from <foo>
statements and those <bar>
from import <bar>
that are not preceded by from <foo>
.
To translate the above in regex, I wrote:
from (\w+)|(?<!from \w+)import (\w+)
The problem seems to be with the non-fixed width of the negative lookbehind but I cannot seem to be able to fix it.
EDIT:
As a bonus, it would also be nice to capture multiple includes as in:
import sys, glob