1

I'm just starting to learn Python and I'm having a hard time testing things out in the terminal. What I want to do is simply run a pre-written Python method in the Python interpreter. (I know how to run it by doing python file_name.py, but I want to run it in the interpreter itself).

So if I for example had the file "exampleModule.py":

def exampleFunc(data):
    print(data)

Then in the terminal I run Python and do:

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import exampleModule
>>> exampleFunc('Hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'exampleFunc' is not defined

The thing that I don't get about this is that if I run the module in the Python IDLE, I can access the exampleFunc, but not in the terminal interpreter.

Thanks!

Sam
  • 67
  • 8
  • You're running Darwin? Cool! I'd like to get it set up myself--may I contact you for pointers? – Fred Barclay Dec 22 '15 at 01:11
  • Oh gosh, I'm super honored, but that's the Unix fork OS X runs on. Sorry @FredBarclay, though I'd totally recommend Linux Mint. – Sam Dec 23 '15 at 05:47
  • Oh, so is that what OS X terminal looks like? BTW: I absolutely agree on Mint--I'm on LMDE Betsy and loving it! :) – Fred Barclay Dec 23 '15 at 22:55
  • Yep! It's a pretty useful terminal, though definitely restricted. Cool, I'll check that out the next time I make a VM! – Sam Dec 25 '15 at 15:15

1 Answers1

1

When you do

import exampleModule

you have to write the full name of its functions. According to the Docs*,

This does not enter the names of the functions defined in exampleModule directly in the current symbol table; it only enters the module name exampleModule there. Using the module name you can access the functions

If you want to write only the function name, do

from exampleModule import *

As, according to the Docs*

This does not introduce the module name from which the imports are taken in the local symbol table (so in the example, exampleModule is not defined).

**changed the function name to yours for better understading.*

rafaelc
  • 57,686
  • 15
  • 58
  • 82
  • Or use the more explicit import -- `from exampleModule import exampleFunc` – Brendan Abel Dec 21 '15 at 23:07
  • 2
    Oh god that was simple (and makes total sense). Thank you so much! – Sam Dec 21 '15 at 23:07
  • I still cannot stress this enough though: https://stackoverflow.com/questions/2386714/why-is-import-bad `import *` puts a lot of things into your namespace you don't expect. It's much better to be explicit and say `from package import module` or `from module import class`. – Alex Huszagh Dec 21 '15 at 23:12
  • 1
    In general, I do things like `import example_module as em` and then `em.some_func`. With name completion, as in IDLE, typing `em.` and pausing brings up a box with a list of all names in `em`, and only names in `em`. – Terry Jan Reedy Dec 21 '15 at 23:13