-2

Let's say I want to explore import statements in Python. How, from the Python command line, can I find the file in which import is defined? Note I am working in Python 2.7.6 (iPython) in Windows 7.

For most objects, just entering the object name is enough. For instance:

import os
os

Yields the following:

<module 'os' from 'C:\Anaconda\lib\os.pyc'>

But you cannot do the same with basic commands like import.

I have tried searching my Python folder but unsurprisingly don't get something as simple as C:\Anaconda\lib\import.py. Is there a simple way to find out where such statements are defined (I realize much of the time it will be in c-code, but that is what I am after)?

Update (5/27/14)

It seems people think it cannot be done in any simple way with a built-in command. However, if your life depended on it, you could write up some inelegant grep-type function in Python, no?

Community
  • 1
  • 1
eric
  • 7,142
  • 12
  • 72
  • 138
  • You can't. The trick you're using works for __modules__, but import isn't a module, it's a statement. – dano May 21 '14 at 00:09
  • @dano You can't, period, or you can't the way I tried? – eric May 21 '14 at 00:11
  • 1
    I believe `import` is implemented in C, so you'd need to find where that is in the actual Python source. However, starting with Python 3.1, there is the `importlib` module, which provides a pure Python implementation of `import`: https://docs.python.org/3/library/importlib.html – dano May 21 '14 at 00:12
  • 1
    Here is the C implementation: http://hg.python.org/cpython/file/db302b88fdb6/Python/import.c – dano May 21 '14 at 00:13
  • So there is no way to find out what C file it is defined in, from the Python command line? Not sure why this was downvoted...This is something most IDEs for C/C++ let you do pretty easily. – eric May 21 '14 at 00:16
  • No, there is not. This is a little different from what C/C++ IDEs do. What you're asking for would be more like a C++ IDE showing you where the source code for `template` or `#include` is. `import` is part of the language itself. – dano May 21 '14 at 00:19
  • @dano good point on my analogy being off (except for in Matlab, where it is trivial to find where things are defined :) ). Still, not sure my q deserved a downvote (whoever did it). – eric May 21 '14 at 00:23
  • I agree, it's a reasonable question. – dano May 21 '14 at 00:25

2 Answers2

1

import isn't a module the way that os is. Instead, it's a statement.

https://docs.python.org/2/reference/simple_stmts.html#import

khampson
  • 14,700
  • 4
  • 41
  • 43
  • So my way doesn't work, but is there a way to find the file where import is defined? Is it in Python's C code, and if so is there a way to find where, from the python command line? – eric May 21 '14 at 00:12
  • What's the context for why you want to know? (That may lead to a different suggestion.) – khampson May 21 '14 at 00:50
  • I wan to track down the source code that implements a function, so I can study the guts of the language. The example is actually immaterial. It could be "print" or any other command. – eric May 21 '14 at 01:32
  • 1
    Ah. Modifying the link @dano posted above, you might want to check out http://hg.python.org/cpython/file/db302b88fdb6/Python/ for a variety of internal implementations. – khampson May 21 '14 at 01:34
  • That is helpful. One specific I want to know is the details about how __init__.py files are used in the import process. – eric May 21 '14 at 01:37
0

When you call os after importing, it prints the path to the file because it is a module. Instead, import is a statement:

>>> import math
>>> math
<module 'math' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/math.so'>
>>> import
  File "<stdin>", line 1
    import
         ^
SyntaxError: invalid syntax
>>> 

Just in case you feel like you needed to know, here are the other statements that do similar things when called blank:

>>> with
  File "<stdin>", line 1
    with
       ^
SyntaxError: invalid syntax
>>> yield
  File "<stdin>", line 1
SyntaxError: 'yield' outside function
>>> return
  File "<stdin>", line 1
SyntaxError: 'return' outside function
>>> continue
  File "<stdin>", line 1
SyntaxError: 'continue' not properly in loop
>>> import
  File "<stdin>", line 1
    import
         ^
SyntaxError: invalid syntax
>>> raise
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType
>>> assert
  File "<stdin>", line 1
    assert
         ^
SyntaxError: invalid syntax
>>> del
  File "<stdin>", line 1
    del
      ^
SyntaxError: invalid syntax
>>> break
  File "<stdin>", line 1
SyntaxError: 'break' outside loop
>>> 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • Yes, this is what Ken said: the original question was how do you find the source code for such statements, from within Python? It seems nothing simple, as I wrote you'd basically need to build a grep functionality within Python. – eric May 28 '14 at 16:39