Hello,

I am just starting learning Python and I am having some trouble properly importing a function from a module.

I wrote the short script:

def print_twice(param):
    print(param, param)

When I try to import the function print_twice, the shell tells me it cannot import the name print_twice.

When I choose "run module" from the "window" menu, it works fine.

Why is it telling me it cannot import the name print_twice?

This is copied and pasted from the shell (first I try to import, then I "restart" and run the module):

Python 3.1.2 (r312:79360M, Mar 24 2010, 01:33:18)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "copyright", "credits" or "license()" for more information.

>>> import os
>>> os.chdir('/Users/3FLryan/Documents/Programming/Python/HTLCS')
>>> from test import print_twice
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    from test import print_twice
ImportError: cannot import name print_twice
>>> ================================ RESTART ================================
>>> 
>>> print_twice('spam')
spam spam

The current directory is correct.

Thanks!

Recommended Answers

All 9 Replies

Maybe here is reason

import test
print test
"""test is built in module: output:
<module 'test' from 'J:\Python26\lib\test\__init__.py'>"""

Aha. I changed the name but it still doesn't work:

>>> import tryme1
>>> print_twice('spam')
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    print_twice('spam')
NameError: name 'print_twice' is not defined
>>>

Huh?

and what is output of

import trymel
dir(trymel)

??

You should call print_twice:

trymel.print_twice('Finally! ')
import tryme1
dir(tryme1)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'print_twice']

Thanks. Learned a new trick:

After importing a module, you can call a function from that module by [module].[function()]

And I got it to work my way too:

>>> import tryme1
>>> dir(tryme1)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'print_twice']
>>> tryme1.print_twice('Finally!')
Finally! Finally!
>>> ================================ RESTART ================================
>>> import os
>>> os.chdir('/Users/3FLryan/Documents/Programming/Python/HTLCS')
>>> from tryme1 import print_twice
>>> print_twice('spam')
spam spam

Thanks!

If you want to learn why 95% of time them import module form is prefered, you can read this link on namespace polution.

Aha!!! My workaround was copying "tryme1.py" into the directory that IDLE defaulted to (for opening, saving, etc.).

I solved the core of my problem. The problem was even though the current directory was correct, the shell was not searching the current directory for modules. It was only searching in the directories found in sys.path, of which the directories that include my modules are not included.

Here's the fix:

sys.path.insert(0, '/Users/3FLryan/Documents/Programming/Python/HTLCS')

Hooray.

I feel much, much cooler than I should right now.

Thanks, tonyjv. I am just working through a Python tutorial and figured I should be able to do everything in it so that is why I wanted to do it "my" way. I'm sure in the future I'll try to use the more efficient way.

Looks like you are doing well, good luck!

It is not more efficient, but it is more safe way not to at least not to do

from module import *

if the module is not desined to be used that way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.