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!

4
  • You're running Darwin? Cool! I'd like to get it set up myself--may I contact you for pointers? Commented Dec 22, 2015 at 1: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. Commented Dec 23, 2015 at 5: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! :) Commented Dec 23, 2015 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! Commented Dec 25, 2015 at 15:15

1 Answer 1

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.*

Sign up to request clarification or add additional context in comments.

4 Comments

Or use the more explicit import -- from exampleModule import exampleFunc
Oh god that was simple (and makes total sense). Thank you so much!
I still cannot stress this enough though: 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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.