简体   繁体   中英

Import a module from a package

I'm trying to import a simple package, but it doesn't work.

I have a "package" directory that contains two files:

  • foo.py (with a function called fct)
  • __init__.py (with nothing in it)

Here is the content of tests.py :

import package.foo

foo.fct(7)

But it doesn't work. If I change the import line to from package.foo import fct , I can execute the function.

You need import package.foo as foo or one of the alternatives below ...

import package.foo
package.foo.fct(7)

or:

import package.foo as foo
foo.fct(7)

or possibly:

from package import foo
foo.fct(7)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM