Import error when trying to import python module from other folder

Advertisements This is my project structure: /my_project /first_folder first.py /second_folder second.py second.py def hello(): print(‘hello’) first.py from ..second_folder import second second.hello() when i run first.py i get the following error: ImportError: attempted relative import with no known parent package How can I avoid this problem? >Solution : Normally when you run your script as the… Read More Import error when trying to import python module from other folder

Python, Import config.py

Advertisements # config.py white = (255,255,255) # main.py import config print(white) # output: Traceback (most recent call last): File "C:\…\Test\Test2.py", line 2, in <module> print(white) NameError: name ‘white’ is not defined Process finished with exit code 1 # wanted output (255, 255, 255) Process finished with exit code 0 Hello I want to create a… Read More Python, Import config.py

Python multi-level imports is not working

Advertisements Consider this folder structure │ main.py │ +—src │ +—functions │ │ │ hello.py │ │ │ +—models │ │ hello_model.py main.py from src.functions.hello import http_message if __name__ == "__main__": print(http_message("This is a test message").message) hello.py from models.hello_model import HttpMessageModel def http_message(message: str) -> HttpMessageModel: return HttpMessageModel( message=message, code=200, ) hello_model.py from dataclasses import… Read More Python multi-level imports is not working

how to import and call same named functions from 2 folders in 1 project space in python?

Advertisements how to rename the imported file with the same name? There are folders named branch1 and branch2 under PROJECT in our current work project, and there is a file with the same name solution.py in these two folders. # Write your code here from branch1 import do as a from branch2 import do as… Read More how to import and call same named functions from 2 folders in 1 project space in python?

In Python, what's the purpose of giving an alias with the same name as the imported lib?

Advertisements When I was reading the source code of aiohttp, I found the code below: click here for code from .web_exceptions import ( HTTPAccepted as HTTPAccepted, HTTPBadGateway as HTTPBadGateway, HTTPBadRequest as HTTPBadRequest, HTTPClientError as HTTPClientError, HTTPConflict as HTTPConflict, HTTPCreated as HTTPCreated, HTTPError as HTTPError, … ) I don’t understand why we need a import A… Read More In Python, what's the purpose of giving an alias with the same name as the imported lib?

What is the right way to do such import

Advertisements So this is my current folder structure └── Main folder/ ├── subfolder/ │ ├── subfolder_function.py │ └── subfolder_function2.py └── main.py screenshot and these are the contents of each file subfolder_function2.py def subfolder_function2(): print("Hey I’m subfolder_function2.py") subfolder_function.py from subfolder_function2 import subfolder_function2 def my_function_from_subfolderfunc2(): subfolder_function2() main.py from subfolder.subfolder_function import my_function_from_subfolderfunc2 if __name__ == ‘__main__’: print(my_function_from_subfolderfunc2())) Why… Read More What is the right way to do such import