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 ?

Matthieu Veron
  • 290
  • 6
  • 16

3 Answers3

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

rdas
  • 20,604
  • 6
  • 33
  • 46
  • 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' – Matthieu Veron Apr 15 '19 at 08:27
  • Then you better try adding the `lambda` directory to `sys.path` or setting `PYTHONPATH` – rdas Apr 15 '19 at 08:29
  • When you said "If you're working directory is lambda", by working directory, what does you mean exactly ? – Matthieu Veron Apr 15 '19 at 08:34
  • 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. – rdas Apr 15 '19 at 08:36
  • Ok, because I was running the code directly from test_main.py. So I shouldn't do that ? – Matthieu Veron Apr 15 '19 at 08:42
  • Try keeping the import as I said in the answer. And then running the test from `lambdas` folder: `python tests/test_main.py` – rdas Apr 15 '19 at 08:42
  • I ran 'python tests/test_main.py' from lambda folder : lambda : python tests/test_main.py. I got an error : Traceback (most recent call last): File "tests/test_main.py", line 1, in from package_name import lambda_function ModuleNotFoundError: No module named 'package_name' – Matthieu Veron Apr 15 '19 at 08:54
  • Yeah seems you need to modify `sys.path`. In the test file, add the `pacakge_name` directory to `sys.path` as it says in the blog I linked – rdas Apr 15 '19 at 08:56
  • Ok. But is it a normal way to work ? Will I always have to add package_name directory to sys.path each time I'd like to import a module from an other folder ? Or is it something wrong in my files and folders organization in my project ? – Matthieu Veron Apr 15 '19 at 09:04
  • The other option is to set `PYTHONPATH`. Check out this answer: https://stackoverflow.com/questions/19917492/how-to-use-pythonpath – rdas Apr 15 '19 at 09:14
  • Yeah, but I mean, an other way than sys.path or PYTHONPATH. I don't know, it seem weird to me to solve the problem by re setting some environment variables. What if I have many different projects ? Should I set new entries in PYTHONPATH for each project, just to be able to test my module ? I'm sure there is something I'm doing the wrong way here. – Matthieu Veron Apr 15 '19 at 09:34
  • Usually it works. Read here to know how: https://docs.python.org/3/tutorial/modules.html#the-module-search-path It might tell why your setup isn'y working. – rdas Apr 15 '19 at 09:54
  • on the lambda directory : python -m tests.test_main And it works, without adding anything to pythonpath or sys.path. – Matthieu Veron Apr 15 '19 at 09:55
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)
ddor254
  • 1,570
  • 1
  • 12
  • 28
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.

alcantula
  • 169
  • 1
  • 14
  • ty for your comment. I got this error message: from ..package_name.lambda_function import * ValueError: attempted relative import beyond top-level package – Matthieu Veron Apr 15 '19 at 08:30