简体   繁体   中英

How to find the file corresponding to a specific python module?

When a python3 program uses a module "m", what line can I write to make it print the location of the file containing the sourcecode for "m"?

In my specific exemple, I am learning Django by reading the source of an app (django-wiki), which contains the following line:

urlpatterns += url(r'', include('wiki.urls'))

I want to modify the file containing wiki.urls (I cloned django so I assume I have the file somewhere). I thought I found it (src/wiki/urls.py), but then I realized that this file mostly contains compatibility functions, and my changes had no effect. Therefore I am trying to see where exactly python goes to find "wiki.urls". I used find and grep to look for parts of the urls which should be defined in wiki.urls (for example, "signup"), but this only returns this "compatibility" file.

edit : succinctness and clarifying answer

Quick answer:

In your case the line urlpatterns += url(r'', include('wiki.urls')) is adding a url path for Django to evaluate for some app 'wiki'. The source code for the urls.py file if anyone else looking at this question is here .

Long answer:

In wiki.urls.py

# line 51
def get_urls(self):
   ... # gets the root urls & adds all other url patterns
   return urlpatterns

# line 249
def get_pattern(app_name="wiki", namespace="wiki", url_config_class=None):
... # warnings etc...
if url_config_class is None:
    url_config_classname = getattr(settings, 'URL_CONFIG_CLASS', None)
    if url_config_classname is None:
        url_config_class = WikiURLPatterns
    else:
        ... # more warning etc...
        url_config_class = import_string(url_config_classname)
urlpatterns = url_config_class().get_urls()

return urlpatterns, app_name, namespace

and in wiki.conf.settings we find

# line 273
#: Dotted name of the class used to construct urlpatterns for the wiki.
#: Default is wiki.urls.WikiURLPatterns. To customize urls or view handlers,
#: you can derive from this.
URL_CONFIG_CLASS = getattr(
    django_settings,
    'WIKI_URL_CONFIG_CLASS',
    None)

So, you have indeed found the correct file (src.wiki.urls.py). From the above segments, you can see that conf.settings defines URL_CONFIG_CLASS as None by default. Then, if None the url conf defines the class as WikiURLPatterns (line 249) and includes the urls as defined in get_urls (line 51). It would be here in get_urls (and the preceding lines) you would want to add your own code to other urls patterns, views, and apps.

However, these settings are becoming depreciated, see here in the docs. So, digging around in the Git for the project you can see that they have reimplemented this for modern Django. So, in wiki.sites.py see line 75 onwards for how urls are instantiated.

You also questioned where sign-up was? Check line 94 in wiki.sites.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM