Python Forum
Correct way to import from a standalone module in a different location
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Correct way to import from a standalone module in a different location
#1
Hello everyone.

I have the following setup:
  • 3 separate AWS Lambda functions, written in Python
  • A small Flask app to test and run the lambda functions locally

The 3 Lambda functions are standalone modules with their requirements pip installed in their folders.
The Flask app is a single app.py file.

Here is the directory structure.
Output:
└── MyProject ├── lambda1 ..folders of libraries like lxml, requests etc... ├── handler.py └── __init__.py ├── lambda2 ..folders of libraries like lxml, requests etc... ├── handler.py └── __init__.py ├── lambda3 ..folders of libraries like lxml, requests etc... ├── handler.py └── __init__.py ├── app.py └── __init__.py
I am trying to import a function from each lambda file, to be used in the Flask app. I do it like so:
from lambda1.handler import some_function
In each lambda file I have an import statement like:
from some_file import something #  This is inside the lambda folder
Whenever I try to run the Flask app I get the following error:
Error:
File "../lambda1/handler.py", line 7, in <module> from some_file import something ModuleNotFoundError: No module named 'some_file'
The question is: How can I import a function from a standalone, separate module I am working on?

Thanks Exclamation

I think I just solved this myself...
I had to change the import to:
from .some_file import something #  This is inside the lambda folder
I spent so much time trying different things! So simple at the end.
Reply
#2
import sys
sys.path.append("/the_path_to_your_module/")
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
As I mentioned above in the Edit I changed the import to have a "." in the beginning. This fixed the Flask app.
The problem now is that if I want to run the lambda separately I get the following error:
Error:
ModuleNotFoundError: No module named '__main__.some_file'; '__main__' is not a package
I have an __init__.py in each Lambda folder.

@wavic, your suggestion worked. I did this for all 3 lambda folders.
Reply
#4
you need a top level __init__.py module, which has to be populated with the data structure of dependencies.
It will look a lot like your ' Here is the directory structure. '
ex (partial):
MyProject/
    lambda1/
        handler.py
        ...
    lambda2/
    ...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Tiny web server as standalone executable? Winfried 0 311 Feb-09-2024, 11:48 AM
Last Post: Winfried
  is import cointegration_analysis a recognized module mitcht33 1 424 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  problem in import module from other folder akbarza 5 1,380 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 710 Aug-06-2023, 01:09 AM
Last Post: aupres
  import module error tantony 5 3,433 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs
  Import a module one step back of the path prathampatel9 1 1,062 Sep-21-2022, 01:34 PM
Last Post: snippsat
  Import a module for use in type hint? Milosz 0 1,477 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Can't install nor import delorean module Tek 3 2,792 Oct-27-2021, 03:32 AM
Last Post: Tek
  import module with syntax error Skaperen 7 5,252 Jun-22-2021, 10:38 AM
Last Post: Skaperen
  'urllib3' Module not found when import 'requests' spanz 5 10,150 Jan-06-2021, 05:57 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020