I have the following directory structure, in Ubuntu:
/Test/Foo
/Test/Foo/foo.py
If I am in /Test
, and I run python
from the command line, followed by from Foo import foo
, I get the following error: ImportError: No module named Foo
.
But this is very confusing, since according to here , one of the directories used to search when importing is the directory from which the script was invoked. If I print out sys.path
though, it does not include /Test
, it just includes other standard Python directories.
Any idea what is going on?
If I'm getting you right, what you are trying to achieve here is the Foo
to be a package and foo
be a module.
Since Foo
is not made into a package (You don't have __init__.py
in the directory), it is not recognized as a package and thus not imported.
When you move into /Test/Foo
, then you are simply importing the module foo
, which will work.
What you possibly need to do here is to create an __init__.py
file inside /Test/Foo
and then import the module from the package.
Or you can try relative imports. Something like from .Foo import foo
.
If you just need a function try this (python 2.7):
sys.path.insert() inserts the directory specified in the path python uses to find files.
commify.py is a file in subdirectory xyz that contains a function commify(value)
import os
import sys
sys.path.insert(0, os.getcwd() + r'\xyz')
from commify import commify
print commify(12345678)
output: 12,345,678
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.