1

I'm making a lambda function in python. Here is the current structure of my project.

lambda/
|-- package_name/
|   |-- __init__.py
|   |-- lambda_function.py
|   |-- a.py 
|   |-- utils.py
|
|-- tests/
|   |-- __init__.py
|   |-- test_main.py
|-- setup.py
|-- README

I would like to import lambda_function.py and a.py in test_main.py

I tried

from a import *
import a
from package_name import a

and some others, but nothing is working.

Could you explain me what is the right solution, and why what I tried actually deosn't work ?

3 Answers 3

2

If your working directory is lambda, try:

from package_name import a

There are some pre-determined places where python will look for packages. Usually the working directory is one of them.

See: https://leemendelowitz.github.io/blog/how-does-python-find-packages.html

Sign up to request clarification or add additional context in comments.

13 Comments

Sorry I forgot to put it in my first message, vut I already tried this one too. I got this error message : from package_name import a ModuleNotFoundError: No module named 'package_name'
Then you better try adding the lambda directory to sys.path or setting PYTHONPATH
When you said "If you're working directory is lambda", by working directory, what does you mean exactly ?
Wherever you're running the tests from. If you're running it from an IDE, there would be a setting to specify which directory. Usually it's the project root.
Ok, because I was running the code directly from test_main.py. So I shouldn't do that ?
|
0

I would recommend you to read about import in Python docs :
Python 3: https://docs.python.org/3/reference/import.html Python 2: https://docs.python.org/2/tutorial/modules.html

but you can import it relatively using : import * from ../package_name/a, import * from ../package_name/lambda_function

or you will need to add package_name folder to the sys.path and then just import * from a as you tried .

adding to sys.path could be done with :

import sys
module_path = "the path to your package_name"
sys.path.insert(0, module_path)

Comments

0

from ..package_name.lambda_function.a import *

What you try is not working because the file you want to access is in the different directory.

1 Comment

ty for your comment. I got this error message: from ..package_name.lambda_function import * ValueError: attempted relative import beyond top-level package

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.