Cannot import file/folder as object
See original GitHub issueAbout
Now, many python developers use typing and find circular import error. So we avoid this by import file/folder as object. However, CPython allow this way but Nuitka not allow now.
Problem
- Nuitka 0.6.5
- Python: 3.6.7 64-bit
- Windows x86_64
I try to compile with fastAPI and find error.
Traceback (most recent call last):
File "C:\Users\nicert\PycharmProjects\bc4py\fast.dist\fast.py", line 1, in <module>
File "C:\Users\nicert\PycharmProjects\bc4py\fast.dist\fastapi\__init__.py", line 7, in <module fastapi>
File "C:\Users\nicert\PycharmProjects\bc4py\fast.dist\fastapi\applications.py", line 3, in <module fastapi.applications>
File "C:\Users\nicert\PycharmProjects\bc4py\fast.dist\fastapi\routing.py", line 8, in <module fastapi.routing>
File "C:\Users\nicert\PycharmProjects\bc4py\fast.dist\fastapi\dependencies\utils.py", line 23, in <module fastapi.dependencies.utils>
File "C:\Users\nicert\PycharmProjects\bc4py\fast.dist\fastapi\utils.py", line 5, in <module fastapi.utils>
ImportError: cannot import name 'routing'
FastAPI use this technique to avoid circular imports. problem
Simple example
import_bug
├── __init__.py
├── a.py
└── b.py
# a.py
from import_bug import b
from typing import Optional
class A:
def __init__(self):
self.b: Optional[b.B] = None
def set(self, new):
self.b = new
# b.py
from import_bug import a
from typing import Optional
class B:
def __init__(self):
self.a: Optional[a.A] = None
def set(self, new):
self.a = new
# bugger.py
from import_bug.a import A
from import_bug.b import B
if __name__ == '__main__':
a = A()
a.set(B())
print(a, a.b)
compile by
python -m nuitka --mingw64 -j 2 --show-progress --show-scons --recurse-all --standalone bugger.py
error
Traceback (most recent call last):
File "C:\Users\nicert\PycharmProjects\nicert\bugger.dist\bugger.py", line 1, in <module>
File "C:\Users\nicert\PycharmProjects\nicert\bugger.dist\import_bug\a.py", line 1, in <module import_bug.a>
File "C:\Users\nicert\PycharmProjects\nicert\bugger.dist\import_bug\b.py", line 1, in <module import_bug.b>
ImportError: cannot import name 'a'
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Importing files from different folder - python - Stack Overflow
If you run it from your root folder (ie. application folder), you are probably fine with sys.path.append('.') then importing the module by using...
Read more >Not able to import the Repository folder object xml file ... - Search
This error occurs when the source folders referenced in the XML file are not mapped properly to the folders in the target repository....
Read more >Python — How to Import Modules From Another Folder? - Finxter
The most Pythonic way to import a module from another folder is to place an empty file named __init__.py into that folder and...
Read more >Cannot import a folder with several Excel files fr...
I try to import a folder (with 15 excel files) from a network location into Power Bi and get the error message "Operation...
Read more >Import files into Figma
This could be the file browser, or a specific Figma file. Locate and select the file(s) you want to import. These could be...
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
I am having the same problem with FastAPI. It appears to be a run-time problem with the way the ‘from {module} import {sub-module}’ statement behaves. It does not appear to be a compile time problem. I am seeing that the ‘routing.py’ submodule is being compiled and linked by Nuitka, but the program still complains that it is not able to find it at run-time.
I am also using Uvicorn and I can say that the problem with Uvicorn and the problem with FastAPI are not the same. The Uvicorn issue seems to be solved merely by using ‘include-module’ directives. This is my compile command:
However, neither the ‘include-module’ nor the ‘include-package’ directives are helpful in solving the FastAPI run-time issue. This is my compile command with FastAPI:
This compiles fine and includes the ‘fastapi.routing’ submodule as demonstrated by the Nuitka and Scons output"
The object file also appears in the linker input at ‘@sources.tmp’:
However, when the program is run, it fails immediately on startup with the following error:
@namuyan seems to have worked around this issue by modifying FastAPI. However, this is not an uncommon import pattern and modifying the upstream FastAPI library is not really a good option for me.
Any specific ideas about why this fails and run-time and how we could fix in Nuitka, versus modifying the compiled code?
The test code being compiled is attached. fastapi_test.zip
This has been solved through a duplicate, cyclic name imports have been working for a while now.