cannot import name 'a' from 'tools' - python

I can run test_pkg1.py and test_pkg2.py
but when I run test.py
Error occurred at test_pkg1.py
Exception has occurred: ImportError
cannot import name 'a' from 'tools' (unknown location)
root
├─ pkg1
│ ├─ tools
│ │ ├─ __init__.py
│ │ └─ a.py
│ ├─ __init__.py.py
│ └─ test_pkg1.py
├─ pkg2
│ ├─ tools
│ │ ├─ __init__.py
│ │ └─ b.py
│ ├─ __init__.py.py
│ └─ test_pkg2.py
├─ test.py
└─ d.py
test_pkg1.py
from tools import a
test_pkg2.py
from tools import b
test.py
from pkg2 import test_pkg2
from pkg1 import test_pkg1

In Python, import paths are always relative to the directory where the main program is executed unless you explicitly use a relative import by preceding the package name with a dot, which then makes the interpreter look for the package in the same directory as the module where the import is made:
test_pkg1.py:
from .tools import a
You also need to create an __init__.py file in the directory where the main program runs in order for that directory to be considered a package under which relative imports can be made.

Related

Define setuptools path for each import module

Given the following project structure:
root
└── dir1
├─ setup.py
├─ script1.py
├── processor
│ ├─ preprocessor.py
│ └─ postprocessor.py
│
├──src_dir
│ ├─ predictor.py
│ └─ script2.py
│
I would like the custom package to ONLY contain the two scripts inside the processor folder and the predictor.pyscript. Moreover, the scripts should be accessible via from processor.preprocessor import ... while the predictor to be in the main root and therefore importable as from predictor import ....
I I should then run the following setup script: python setup.py sdist --formats=gztar:
from setuptools import setup
setup(name="processors",
version="0.1",
...
)

Is there something like esbuild for serverless python?

I come from working with nodejs and serverless framework, where I can configure esbuild so that functions that require other functions are in a single bundle file.
Now I am working python with serverless framework, however, when I import an external function, and deploy. In the function bundle the whole project is uploaded. Clearly this would increase costs and delay the startup of the functions.
I have been reading about mock plugins serverless-python-individually, serverless-package-python-functions but i have not been able to package the function and its dependencies individually.
Is there any way to package the dependencies of the lambda functions individually?
example:
In nodejs I have the following structure:
api/
├─ v1/
│ ├─ handler/
│ │ ├─ fileHandler.js
│ ├─ anotherlambda/
│ │ ├─ index.js
│ ├─ s3Storage/
│ │ ├─ index.js
serverless.yml
When I deploy, it looks like this:
api/
├─ v1/
│ ├─ handler/
│ │ ├─ fileHandler.js
Where in fileHandler.js the code of the s3Storage/index.js dependency is inserted.
In python I have the following structure:
api/
├─ v1/
│ ├─ handler/
│ │ ├─ fileHandler.py
│ ├─ anotherlambda/
│ │ ├─ hello.py
│ ├─ s3Storage/
│ │ ├─ index.py
I would like to know if there is a way to package it in the same way as with nodejs esbuild.

AttributeError: module 'MyModule' has no attribute 'Module1'

I am trying to turn a project of mine into a package so I can deploy it as a wheel.
I have a project directory setup like this:
ProjectDir
├── setup.py
├── MyModule
│ ├── __init__.py
│ ├── Module1
│ │ ├── __init__.py
│ │ ├── main.py
│ ├── Module2
│ │ ├── file1.py
│ │ ├── __init__.py
│ │ ├── file2.py
│ │ ├── file3.py
│ └── Module3
│ ├── __init__.py
│ ├── Sub1
│ │ ├── file1.py
│ │ ├── file2.py
│ │ ├── main.py
│ └── Sub2
│ ├── file1.py
│ ├── main.py
└── test
├── test_Module_1
│ ├── __init__.py
│ └── test_main.py
├── test_Module_2
...
Top level __init__.py is empty
Module 1 __init__.py file
from main import Function1
Similar for other module __init__.py files
setup.py
from setuptools import setup, find_packages
import os
import pip
setup(name='MyModule',
description='Tool suite to run MyModule',
packages=['MyModule'])
I can import MyModule but when I attempt to access any submodule, I get the following
AttributeError: module 'MyModule' has no attribute 'Module1'
Or if I check attributes of my module, none are found.
import MyModule
dir(MyModule)
['builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec', 'os']
This is expected because by default submodules are not imported.
You should import them like so to use them:
import MyModule.Module1
To change this you have to tweak the MyModule/__init__.py file by adding:
import MyModule.Module1
This way, MyModule.Module1 will be available when you import MyModule, as the __init__.py file is executed.

flask run importerror raised

I was running flask app using app.run till now. But when I was trying to use flask-migrate, it throws an error when I am trying 'flask run'
File directory is like this:
project/
├─ project/
│ ├─ static/
│ │ ├─ app.js
│ ├─ templates/
│ │ ├─ base.html
│ ├─ routes.py
│ ├─ __init__.py
│ ├─ forms.py
│ ├─ models.py
├─ run.py
├─ setup.py
├─ MANIFEST.in
I was following documentation, but it did not work. 'python run.py' works but 'flask run' is not working.
export FLASK_APP=project
export FLASK_ENV=development
Error: While importing 'project', an ImportError was raised.
How can I solve this problem?
try, to change the FLASK_APP value
export FLASK_APP=run
...
As far as I understood from the Flask docs - the FLASK_APP value needs to be the same as the "main app" filename (just without the .py)
docs for ref: https://flask.palletsprojects.com/en/2.0.x/quickstart/#a-minimal-application3

Importing within project - modules not working

I have a project with the current structure, but some of my imports are not working when I think they should be. Shoudn't these imports work since the folders are properly marked as modules?
foo
├── app
│ ├── app.py
│ ├── folder1
│ │ ├── aaa.py
│ │ └── __init__.py
│ ├── folder2
│ │ ├── bbb.py
│ │ ├── __init__.py
│ ├── folder3
│ │ ├── ccc.py
│ │ ├── __init__.py
│ ├── __init__.py
│ └── main.py
├── README.md
└── .gitignore
WORKS
aaa.py
class X():
pass
main.py
from folder1.aaa import X
PWD: foo folder
CMD: python app/main.py
DOES NOT WORK
aaa.py
class X():
pass
main.py
from app.folder1.aaa import X
PWD: foo folder
CMD: python app/main.py
Traceback (most recent call last):
File "foo/app/main.py", line 1, in <module>
from app.folder1.aaa import X
ModuleNotFoundError: No module named 'app'
DOES NOT WORK
aaa.py
from app.folder2.bbb import Y
class X(Y):
pass
bbb.py
class Y():
pass
main.py
from folder1.aaa import X
PWD: foo folder
CMD: python app/main.py
File "foo/app/folder1/aaa.py", line 1, in <module>
from app.folder2.bbb import Y
ModuleNotFoundError: No module named 'app'
Python import works by searching the paths in sys.path.
check whether app is added to sys.path by running the below code
import sys
print(sys.path)
if it is not present in this list, append sys.path by including app directory.
import sys
import os
current_loc = os.path.realpath(__file__)
parent_dir = os.path.dirname(os.path.dirname(current_loc))
sys.path.append(parent_dir)

Categories

Resources