0

I have installed a custom module I created myself for learning. If I do

from my_first_module import test
test.thisprintssomething()

The function will work, but if I do

import my_first_module
test.thisprintssomething()

Python spits out, NameError: name 'test' is not defined. How would I import without using "from"?

EDIT:

I've fixed it myself. I forgot to add an "import test" line to my init.py in my_first_module module.

4

2 回答 2

6

since you are importing my_first_module you have to tell the code,, test belongs to my_first_module

import my_first_module

my_first_module.test.thisprintssomething()

for more clarification you can have a look at Importing Python Modules

于 2012-08-27T09:53:37.500 回答
3
import my_first_module

my_first_module.test.thisprintssomething()

这将起作用。在http://docs.python.org/tutorial/modules.html阅读更多内容

于 2012-08-27T09:54:40.730 回答