import package from an other package - python-3.x

I have a very simple project architecture:
project/
__init__.py
dira/
__init__.py
a.py
dirb/
__init__.py
b.py
All the __init__.py are empty.
a.py only contains a "hello" function:
def hello(): return "hello world"
I want b.py to use the hello function from a.py.
What I tried so far:
Following answers from this question, I tried:
import ..dira.a as aa
aa.hello
But when running b.py I get a syntax error on ..a.a
I also tried to manually add the project directory to sys.path and then run:
import dira.a as aa
aa.hello
But when running b.py I get:
ImportError: No module named 'a'
What should I do? Why the solution I found doesn't work?

Short answer:
import ..dira is invalid syntax and only from ..dira.a import hello works
Working configuration: b.py:
from ..dira.a import hello
print(hello())
Also, from docs:
"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."
So if you do relative imports you cannot call b.py directly. Apperently, you cannot do it from project level either. (Python interpreter in project folder, call import dirb.b)
An error occurs:
ValueError: attempted relative import beyond top-level package
But calling import project.dirb.b makes proper output:
hello world
EDIT: Why import project.dirb.b can be called only outside of project module?
Let's look at the problem from interpreter's side.
You call import project.dirb.b. It runs dirb/init.py, 'current module' is project.dirb
Interpreter finds command from ..dira.a import hello
It treats double dot ..dira.a as "move one module up and go to dira and import a.hello". 'Current module' is project.dirb, so parent module is project, thus it finds parent.dira
What happens when you call import dirb.b directly? It fails on trying to go level up in module hierarchy and reports error.
Thus, relative import makes project.dirb.b importable only outside of project.

Related

ModuleNotFoundError: No module named 'module' while importing my own module VsCode MacOs Python 3.10

Structure:
|--folder/
|--a.py
|--main.py
When loading module 'main' into module 'a'
#a.py
import main
the following error occurs - ModuleNotFoundError: No module named 'main'.
PyCharm copes with this task, but VSCode does not. What's the matter?
Looking at your directory structure, I presume that a.py and main.py are in different directories. If that is the case, then the answers to [this question][1] should be useful. To elaborate a bit further, you can use the sys module to specify the path to the other module (main.py) i.e. if main.py is in a different directory from a.py.
import sys
sys.path.append('/path_to_main_module_directory/')
import main
print("This import is for the main module")
I hope this helps!
[1]: Importing files from different folder

How can I import a module from a parent directory?

I have the following directory structure for my Python project:
lib
__init__.py
tasks.py
api(directory)
__init__.py
app.py
Now, I want to import the tasks module into app.py. If I just type
import tasks
it magically works in Pycharm, but when executed via bash I get an error message saying "No module named tasks". I also tried
from .. import tasks
which gives me
ValueError: attempted relative import beyond top-level package
What am I doing wrong? Why does import tasks work when executed in Pycharm?

Importing custom module cleanly

I would like to import a class from my submodule without having to use the from submodule.submodule import Class syntax. Instead I would just like to do from submodule import Class like a normal Python3 module.
I feel this should have been answered a million times, and while there are several similarly named questions on SO, none of them provide a clear, simple solution with a bare-bones example.
I'm trying to get the simplest test working with this setup:
.
├── main.py
└── test
├── __init__.py
└── test.py
In my test module, I have the following contents:
test.py
class Test:
def __init__(self):
print('hello')
__init__.py
from test import Test
__all__ = ['Test']
In the upper-level main.py I have the following:
from test import Test
Test()
When I try to run main.py I get:
ImportError: cannot import name 'Test'
I know I could replace the import statement in main.py with from test.test import Test, but my understanding was that one of the points of __init__.py was to make submodules accessible at the package level (with __all__ allowing to import all with from test import *)
According to PEP 404:
In Python 3, implicit relative imports within packages are no longer
available - only absolute imports and explicit relative imports are
supported. In addition, star imports (e.g. from x import *) are only
permitted in module level code.
If you change __init__.py to:
from test.test import Test
__all__ = ['Test']
then your code works:
$ python3 main.py
hello
But now it works only on python3 (and your original code works only on python2).
To have code that works on both lines of python, we have to use explicit relative import:
from .test import Test
__all__ = ['Test']
Code execution:
$ python2 main.py
hello
$ python3 main.py
hello

ModuleNotFoundError: No module named '__main__.xxxx'; '__main__' is not a package

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.

Cannot import my module into another module

Professional Python newbie here. I have created a Python module called aviation with a file in there called database.py.
I have another module called core, with a file in there called calculator.py.
I want to import aviation.database.py into my calculator.py.
The basic structure is as follows:
My project
aviation (module)
- database.py
core (module)
- calculator.py
test.py
My calculator.py file has an import such as:
from aviation import database as aviation_database
This module is not recognised and I get a red squiggly line indicating as much.
If I create another file test.py outside of aviation and core and add the above import, there are no issues in this tests.py file - the import works fine.
It appears that I need to do something so that my module can import from another module... it does allow me to import installed modules (like date), but I have no idea what I am missing.
I am using the IntelliJ IDE and my code is located in the regular C:\Users\\IdeaProjects directory.
Can someone tell me what I should do and why I am facing this problem?
Well I would recommend you to create __init__ file in the modules.
The python3 doc states that :
The __init__.py files are required to make Python treat the
directories as containing packages; this is done to prevent
directories with a common name, such as string, from unintentionally
hiding valid modules that occur later on the module search path. In
the simplest case, init.py can just be an empty file, but it can
also execute initialization code for the package or set the all
variable, described later.
So your Directory Structure becomes
My project
aviation (module)
- database.py
- __init__.py
core (module)
- calculator.py
- __init__.py
test.py
Now import all the thing in your __init__ file that you want to us from other packages. Like in aviation's __init__.py file write
import database
Similarly, for core's __init__.py
Now in calculator.py you can import them by -
from aviation import database
Let me know, if this help !
Thanks
Its as simple as this.
For instance, you have an already written code e.g myname.py and you want to import the entire module into new code e.g newwork.py
step 1. open your code newwork.py
step 2. just as you do other import, just write:
import myname
myname
Thats all.
Now, you have your entire nyname code inside newwork. When you run newwork, it runs nymame alongside.
I hope this help somebody?

Resources