Cant import from module despite presence of __init__.py

2024/11/18 16:26:22

I have the following folder structure

project_folder/pyutils/__init__.pyscript1.pyscript2.py lambdas/__init__.pylambda_script1.pylambda_script2.pylambda_tests/__init__.pylambda_test1.py

Within lambda_test1.py I have the following attempts

from lambdas.lambda_script1 import * # errors saying no module named lambdas
from .lambdas.lambda_script1 import * # errors saying ModuleNotFoundError: No module named '__main__.lambdas'; '__main__' is not a package
from ..lambdas.lmabda_script1 import * # errors saying tried to import above top level path

I'm trying to run my tests from the project folder with a command like

python pyutils/lambda_tests/lambda_test1.py

But none of the options seem to work

If I run IPython from within the pyutils folder and run from lambdas.lambda_script1 import * it works. Is this a python path problem?

I also tried adding an __init__.py at the project folder and it still didn't work

Answer
project_folder/pyutils/__init__.pyscript1.pyscript2.py lambdas/__init__.pylambda_script1.pylambda_script2.pylambda_tests/__init__.pylambda_test1.py

Firstly, export your pythonpath to be the root of your project

export PYTHONPATH=/path/to/project_folder/py_utils

Then you would use imports like:

import script1

import script2

import lambdas.lambda_script1

import lambdas.lambda_script2

import lambdas_tests.lambda_test1

https://en.xdnf.cn/q/118652.html

Related Q&A

Multiple strings in one variable

Say, for example, I have some code that goes like this: sentence = input("Enter input: ") target_letter = "a" or "b" or "c" print(sentence.index(target_letter))M…

How to get the area of shape filled using turtle

I graphed a fractal shape in Python using turtle, and am trying to get the area of this fractal after a sufficiently high iteration. This fractal is related to the Koch snowflake, for those interested.…

What distinguishes a command from needing () vs not?

I recently spent way too long debugging a piece of code, only to realize that the issue was I did not include a () after a command. What is the logic behind which commands require a () and which do not…

python iterate yaml and filter result

I have this yaml file data:- name: acme_aws1source: awspath: acme/acme_aws1.zip- name: acme_gke1source: gkepath: acme/acme_gke1.zip- name: acme_ocisource: ocipath: acme/acme_oci1.zip- name: acme_aws2so…

Faster way to looping pixel by pixel to calculate entropy in an image

I have been calculating the entropy of an image with a pixel by pixel convolution operation, and it has been working but very slowly, increasing the execution time with the kernel size. Here is my func…

Google AppEngine - updating my webapp after deploy

friends! Im fairly new to web app world and I have a question regarding Google AppEngine functions. Ive installed the Launcher on my machine and signed up for the online platform (Python). Ive added …

Extract GPS coordinates from .docx file with python

I have some hectic task to do for which I need some help from python. Please see this word document.I am to extract texts and GPS coordinates from each row. There are currently over 100 coordinates in …

How to Serialize SQL data after a Join using Marshmallow? (flask extension)

I have 2 tables in SQL: class Zoo(db.Model):id = db.Column(db.Integer, primary_key=True)nome = db.Column(db.String(80), unique=True, nullable=False)idade = db.Column(db.Integer, unique=False, nullable=…

Python readline() is not reading a line with single space

I am reading a text file using readline(). My file contains below content with a space at second line:!" # $ % & When I print read values using-print("%s\n" % iso5_char)It prints-!&q…

Bisection search [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Using bisection search to determine I have posted other thread but it did not receive answers thus im trying to provide so…