Relative import
See original GitHub issueI’m trying to use nuitka for compiling a package which contains runnable modules, but it doesn’t work.
Dummy example:
Directory structure:
nuitka_test/
├── __init__.py
├── __main__.py
├── module1.py
├── module2.py
File content
nuitka_test/module1.py
:
def func():
print("I'm in module1.func")
print("I'm in module1")
nuitka_test/module2.py
:
from . import module1
print("I'm in module2")
if __name__ == "__main__":
print("I'm in module2, if __main__ ")
module1.func()
nuitka_test/__main__.py
:
from . import module2
print("I'm in nuitka_test __main__")
nuitka_test/__init__.py
:
# empty
Nuitka commands
Here is what I tried, each with its output:
–standalone on nuitka_test
$ python -m nuitka --standalone nuitka_test
Nuitka-Scons:INFO: Backend C compiler: gcc (gcc).
Nuitka-Scons:INFO: Compiled 11 C files using ccache.
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache hit (direct)': 4
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache hit (preprocessed)': 6
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache miss': 1
$ ls
nuitka_test/ nuitka_test.build/ nuitka_test.dist/
$ nuitka_test.dist/nuitka_test
Traceback (most recent call last):
File "/home/sapo/Develop/nuitka_test/nuitka_test.dist/__main__.py", line 1, in <module>
ImportError: attempted relative import with no known parent package
–standalone on nuitka_test/module2.py
$ python -m nuitka --standalone nuitka_test/module2.py
Nuitka-Scons:INFO: Backend C compiler: gcc (gcc).
Nuitka-Scons:INFO: Compiled 11 C files using ccache.
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache miss': 11
$ ls
module2.build/ module2.dist/ nuitka_test/
$ module2.dist/module2
Traceback (most recent call last):
File "/home/sapo/Develop/nuitka_test/module2.dist/module2.py", line 1, in <module>
ImportError: attempted relative import with no known parent package
–module on nuitka_test/module2.py
$ python -m nuitka --module nuitka_test/module2.py
Nuitka-Scons:INFO: Backend C compiler: gcc (gcc).
Nuitka-Scons:INFO: Compiled 10 C files using ccache.
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache hit (direct)': 3
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache hit (preprocessed)': 6
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache miss': 1
$ python -m module2
/usr/bin/python: No code object available for module2
–module on nuitka_test
$ python -m nuitka --module nuitka_test
Nuitka-Scons:INFO: Backend C compiler: gcc (gcc).
Nuitka-Scons:INFO: Compiled 10 C files using ccache.
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache miss': 10
$ ls
module2.build/ nuitka_test/ nuitka_test.cpython-38-x86_64-linux-gnu.so
module2.dist/ nuitka_test.build/ nuitka_test.pyi
$ python -m nuitka_test
/usr/bin/python: No module named nuitka_test.__main__; 'nuitka_test' is a package and cannot be directly executed
$ mv nuitka_test nuitka_test.back
$ ls
module2.build/ nuitka_test.back/ nuitka_test.cpython-38-x86_64-linux-gnu.so
module2.dist/ nuitka_test.build/ nuitka_test.pyi
$ python -m nuitka_test.module2
/usr/bin/python: No module named nuitka_test.module2
Non-relative import
By using import
instead of from . import
, nuitka works when using the --standalone
or no flags, but I still cannot use it as module with --module
. To be compliant with CPython, nuitka should be able to execute a module in all the previous scenarios (compiled mackage, compiled module, standalone module, standalone package).
import
is not portable because the module cannot be exported as package.
System Version
$ python -m nuitka --version
0.6.9.4
Python: 3.8.5 (default, Sep 5 2020, 10:50:12)
Executable: /usr/bin/python
OS: Linux
Arch: x86_64
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Absolute vs Relative Imports in Python
A relative import specifies the resource to be imported relative to the current location—that is, the location where the import statement is. There...
Read more >python - Relative imports for the billionth time - Stack Overflow
Relative imports use the module's name to determine where it is in a package. When you use a relative import like from .....
Read more >5. The import system — Python 3.11.1 documentation
The import statement is the most common way of invoking the import machinery, ... This attribute is used instead of __name__ to calculate...
Read more >Absolute and Relative Imports in Python - GeeksforGeeks
Relative import specifies an object or module imported from its current location, that is the location where import statement resides.
Read more >Understanding relative and absolute imports in Next.js
In a relative import, the file's import path should be relative to where the import statement is. Relative imports generally start with ....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Can you give me a link so that I can test and create an example?
I’m pretty sure this is a duplicate of #493 (adding comment for anyone who comes across this later)