This question already has answers here:
Relative imports for the billionth time
(12 answers)
Closed 1 year ago.
So my file system looks something like this
/projects
--setup.py
--setup.cfg
--/app
--/--__init__.py
--/--app.py
--/--file_with_class.py
In my app.py I import file_with_class.py, but when I use setup to install my module with pip install . and try running it, I get this error: ModuleNotFoundError: No module named 'className'
supose file_with_class.py contains a class named className
I'm importing this class in app.py like this:
from file_with_class import className
Also, when executing app.py on its own it works perfectly, the problem comes when creating the package with setuptools and after installing my own package, trying to run it.
You can use relative import:
from . import class
You should use either relative, or fully qualified module name imports:
from . import class
# or, probably better:
import app.class
from app import class
BTW, class is a reserved word. Not sure if this was the module's name in your original code, but if it was you probably want to avoid it.
Related
This question already has answers here:
Relative imports in Python 3
(31 answers)
Closed 4 months ago.
from ..box_utils import decode, nms
This line is giving error
ImportError: attempted relative import with no known parent package
What is this error and how to resolve this error?
Apparently, box_utils.py isn't part of a package. You still can import functions defined in this file, but only if the python script that tries to import these functions lives in the same directory as box_utils.py, see this answer.
Nota bene: In my case, I stumbled upon this error with an import statement with one period, like this:
from .foo import foo. This syntax, however, tells Python that foo.py is part of a package, which wasn't the case. The error disappeared when I removed the period.
If a different dictionary contains script.py, it can be accessed from the root. For instance:
If your program is structured...:
/alpha
/beta
/delta
/gamma
/epsilon
script.py
/zeta
...then a script in the epsilon directory can be called by:
from alpha.gamma.epsilon import script
in the latest python version, import it, directly don't use .. and .library
import the file which you want. this technique will work in the child directory.
If you import it from parent directory, then place the directory's full path.
package
|--__init__.py
|--foo.py
|--bar.py
Content of bar.py
from .foo import func
...
If someone is getting the exactly same error for from .foo import func.
It's because you've forgot to make it a package. So you just need to create __init__.py inside package directory.
Currently trying to work in Python3 and use absolute imports to import one module into another but I get the error ModuleNotFoundError: No module named '__main__.moduleB'; '__main__' is not a package. Consider this project structure:
proj
__init__.py3 (empty)
moduleA.py3
moduleB.py3
moduleA.py3
from .moduleB import ModuleB
ModuleB.hello()
moduleB.py3
class ModuleB:
def hello():
print("hello world")
Then running python3 moduleA.py3 gives the error. What needs to be changed here?
.moduleB is a relative import. Relative only works when the parent module is imported or loaded first. That means you need to have proj imported somewhere in your current runtime environment. When you are are using command python3 moduleA.py3, it is getting no chance to import parent module. You can:
from proj.moduleB import moduleB OR
You can create another script, let's say run.py, to invoke from proj import moduleA
Good luck with your journey to the awesome land of Python.
Foreword
I'm developing a project which in fact is a Python package that can be installed through pip, but it also exposes a command line interface. I don't have problems running my project after installing it with pip install ., but hey, who does this every time after changing something in one of the project files? I needed to run the whole thing through simple python mypackage/main.py.
/my-project
- README.md
- setup.py
/mypackage
- __init__.py
- main.py
- common.py
The different faces of the same problem
I tried importing a few functions in main.py from my common.py module. I tried different configurations that gave different errors, and I want to share with you with my observations and leave a quick note for future me as well.
Relative import
The first what I tried was a relative import:
from .common import my_func
I ran my application with simple: python mypackage/main.py. Unfortunately this gave the following error:
ModuleNotFoundError: No module named '__main__.common'; '__main__' is not a package
The cause of this problem is that the main.py was executed directly by python command, thus becoming the main module named __main__. If we connect this information with the relative import we used, we get what we have in the error message: __main__.common. This is explained in the Python documentation:
Note that relative imports are based on the name of the current module. Since the name of the main module is always __main__, modules intended for use as the main module of a Python application must always use absolute imports.
When I installed my package with pip install . and then ran it, it worked perfectly fine. I was also able to import mypackage.main module in a Python console. So it looks like there's a problem only with running it directly.
Absolute import
Let's follow the advise from the documentation and change the import statement to something different:
from common import my_func
If we now try to run this as before: python mypackage/main.py, then it works as expected! But, there's a caveat when you, like me, develop something that need to work as a standalone command line tool after installing it with pip. I installed my package with pip install . and then tried to run it...
ModuleNotFoundError: No module named 'common'
What's worse, when I opened a Python console, and tried to import the main module manually (import mypackage.main), then I got the same error as above. The reason for that is simple: common is no longer a relative import, so Python tries to find it in installed packages. We don't have such package, that's why it fails.
The solution with an absolute import works well only when you create a typical Python app that is executed with a python command.
Import with a package name
There is also a third possibility to import the common module:
from mypackage.common import my_func
This is not very different from the relative import approach, as long as we do it from the context of mypackage. And again, trying to run this with python mypackage/main.py ends similar:
ModuleNotFoundError: No module named 'mypackage'
How irritating that could be, the interpreter is right, you don't have such package installed.
The solution
For simple Python apps
Just use absolute imports (without the dot), and everything will be fine.
For installable Python apps in development
Use relative imports, or imports with a package name on the beginning, because you need them like this when your app is installed. When it comes to running such module in development, Python can be executed with the -m option:
-m mod : run library module as a script (terminates option list)
So instead of python mypackage/main.py, do it like this: python -m mypackage.main.
In addition to md-sabuj-sarker's answer, there is a really good example in the Python modules documentation.
This is what the docs say about intra-package-references:
Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.
If you run python3 moduleA.py3, moduleA is used as the main module, so using the absolute import looks like the right thing to do.
However, beware that this absolute import (from package.module import something) fails if, for some reason, the package contains a module file with the same name as the package (at least, on my Python 3.7). So, for example, it would fail if you have (using the OP's example):
proj/
__init__.py (empty)
proj.py (same name as package)
moduleA.py
moduleB.py
in which case you would get:
ModuleNotFoundError: No module named 'proj.moduleB'; 'proj' is not a package
Alternatively, you could remove the . in from .moduleB import, as suggested here and here, which seems to work, although my PyCharm (2018.2.4) marks this as an "Unresolved reference" and fails to autocomplete.
Maybe you can do this before importing the moduleļ¼
moduleA.py3
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from moduleB import ModuleB
ModuleB.hello()
Add the current directory to the environment directory
Just rename the file from where you run the app to main.py:
from app import app
if __name__ == '__main__':
app.run()
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
will solve the issue of import path issue.
This question already has answers here:
How do I import other Python files?
(23 answers)
Closed 5 years ago.
Can I import the test01.py like os, sys module?
I want to import the test01.py like:
import test01.py
In this case now, I only can import it like this:
from testDemo02 import test01
Is it possible to reach it?
It looks like test01 is in the package testDemo02 - you can tell because there is a file __init__.py in the directory testDemo02. Given that, there are a couple possibilities:
If the parent directory of testDemo02 is in the module search path (sys.path), but testDemo02 itself is not, you can import your test01 module using either
import testDemo02.test01
or
from testDemo02 import test01
I suspect this is the case since you tried the latter one and it works. This is what I would expect because I see that __init__.py file there.
If testDemo02 itself is in the search path, you will be able to import your module with just
import test01
I would find it strange for a directory to be in the search path when it also contains an __init__.py file, but it is possible.
You can use sys module's path property to append your path:
>>> import sys
>>> sys.path.append("/testDemo02/test01")
>>> import test01
This question already has an answer here:
A Python module and package loading confusion
(1 answer)
Closed 9 years ago.
I am porting some matlab code to python. I need to work with packages and modules in this case. The relevant package directory structure looks like this:
toppackage
__init__.py
subpackage
__init__.py
module.py
...
In the script I use the package, I can work like this:
from toppackage.subpackage.module import SomeClass
s = SomeClass()
But I would prefer working like this:
import toppackage %somewhere at the beginning of file
s = toppackage.subpackage.module.SomeClass()
I see this is done in numpy. But I could not find it in documentation. How can I do that?
Thanks in advance.
You need to import contained packages in the __init__.py files.
You can import the packages inside the toppackage/__init__.py for example:
import toppackage.subpackage.module
or you can import just each directly contained package, so in toppackage/__init__.py:
from . import subpackage
and in toppackage/subpackage/__init__.py:
from . import module
Just importing the top-level package does not automatically make the contained packages available. You need to explicitly import the full path once, somewhere, before that works.
The numpy package imports the nested packages in the top-level __init__.py.
How this stuff works depends critically on what is in __init__.py. Whatever gets imported in that script becomes part of the package namespace.
For example, if your toppackage/__init__.py is empty, to gain access to subpackage, you'd need to do:
import toppackage
try:
p = toppackage.subpackage
except AttributeError:
print "you would see this"
import toppackage.subpackage
p = toppackage.subpackage #no error now
however, if toppackage/__init__.py included the line:
#toppackage/__init__.py
import subpackage
Then the above script would raise no Exceptions.
As noted in the comments, you can also use relative imports since you are in a package:
from . import subpackage
This avoids "namespace" conflicts -- e.g. if you have a subpackage named os it will get your subpackage rather than the python-level package.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to do relative imports in Python?
I'm experiencing something that seems very random to me.
I have a folder structure much like this:
dir A
__init__.py is empty
a.py imports stuff and b.py
dir B
__init__.py is empty
b.py imports NOTHING
a.py raises an error (cannot import name b). This only happens while b is part of module B.
If I move it outside the directory, the import error does NOT occur.
Any help would be appreciated. I must be overlooking something.
Did you try the relative import
from ..B import b
?
EDIT: This does not apply if it doesn't matter where package B lives.
But you don't describe what exactly you do. As you may know or not, there are several import forms:
import module
import package # imports package.__init__ under the name package
import package.module
from package import module
import package
from module import component
from package.module import component
As you only write
a.py imports stuff and b.py
I don't know what exactly happens: if you try to
import b
that fails because b lives in the package B. So you need one of
from B import b
import B.b
Your comment above mentions a name clash. Which of two equally named packages and modules have priority depends on in which directory you are: '.' is normally at the very start of sys.path, so if you are directly under your utils directory you might have a different experience than otherwise.