For context I'm running this package in Raspberry pi.
I have a package in the below structure
firmware
├── __init__.py
├── button_buzzer.py
├── payload_reader.py
├── payload_writer.py
├── pub_iothub_api.py
├── rs232_serial.py
├── status_led.py
└── system_config.py
I have a module in init.py named logger but when i try to import it throws error ModuleNotFoundError: No module named 'firmware' for specific programs which runs in loop, what is the reason for this?
Here is the program (I have removed contents of code.)
from time import sleep
# Importing custom modules
from firmware import logger
if __name__ == "__main__":
"""
The script runs an infinite loop to constantly check if the button is pressed.
"""
while True:
try:
except Exception as exptn:
logger.exception(exptn)
pass
Related
I am facing issues in writing unit test for my flask app. Exact issue is that test files in unit-test directory is not able to import files from app folder.
My directory structure:
.
├── Dockerfile
├── README.md
├── app
│ ├── __init__.py
│ ├── api.py
│ └── wsgi.py
├── docker
│ ├── docker-compose.yml
│ └── start.sh
├── requirements.txt
└── unit-test
├── __init__.py
└── test_api.py
Code in unit-test/test_api.py:
import unittest
from app import api
Absolute import throws this error:
from app import api
ModuleNotFoundError: No module named 'app'
I have tried the following after checking a few resources of absolute and relative imports, none of them worked.
from .. import app
error:
from .. import app
ImportError: attempted relative import with no known parent package
I checked a few questions on SO and someone recommended having _init_.py file in the unit-test folder as well but I already have a blank init.py file there.
I have reviewed numerous blogs and youtube tutorials and absolute imports work for them easily.
Please advise how to fix this error and run unit tests.
import sys
sys.path.append('/path/to/the/required/folder')
import name_of_file
if not an inbuilt package, Python only searches in the current directory when you try to import, thus we have to add this path, so that it looks inside this particular folder as well. After adding that, you can simply do the import.
I recently developed a package with the following structure:
.
├── Foo
│ ├── module1.py
│ ├── module2.py
│ ├── __init__.py
where we have:
#__init__.py
from module1 import function1, function2
from module2 import function3
#module1.py
def function1():
print("Hello")
def function2():
print("World")
#module2.py
def function3():
print("Hello again")
I wish to be able to import my full package Foo at once from another directory (with no relative path I can relate to).
For instance, I would like to be able to call Foo.module1.function1() in my scripts on another drive.
I cannot use sys.path, and I cannot just move either my script or package into parent/child directories.
I know I can import all modules one by one from a specific directory using importlib (cf. How to import a module given the full path?), and then looping, for instance with glob, but I read that configuring the __init__ file should allow me to import everything at once. Any advice ? (for what it matters, using Python3.9, with no access to PyCharm)
I am new to building packages so bear with me. I am having a problem importing the subpackages of my latest python project.
My directory structure is the following:
├── package
│ ├── __init__.py
│ ├── subpackage_a
│ │ ├── __init__.py
│ │ └── functions_a.py
│ └── subpackage_b
│ ├── __init__.py
│ └── functions_b.py
└── setup.py
The files look as follows
setup.py
:
from setuptools import setup
setup(name='test_package',
version='0.3',
description='',
author='me',
packages=['package']
)
package/__init__.py: empty.
subpackage_a/__init__.py: from .functions_a import *
subpackage_b/__init__.py: from .functions_b import *
functions_a.py
contains
def hello_world_a():
print('hello its a')
and functions_b.py contains
def hello_world_b():
print('hello its b')
Now I open a virtualenv go to the setup.py's directory and I pip install .. I was expecting to access the functions contained in the subpackages a and b. But when I try to import the functions I get a module not found error.
from package.subpackage_a import hello_world_a
ModuleNotFoundError: No module named 'package.subpackage_a'
and the same thing holds for subpackage_b. But if I import package this is recognised. I have a feeling that this approach used to work, as I have some old packages written this way which don't work any longer.
Perhaps I have to change my init.py files ? What am I doing wrong ?
setuptools.setup doesn't know that subpackage_a and subpackage_b exist. You only specified the top-level package. So it won't include these subpackages in the installation. Instead you should also specify them:
setup(
...,
packages=['package', 'subpackage_a', 'subpackage_b']
)
This process can be automatized via find_packages():
from setuptools import find_packages
setup(
...,
packages=find_packages()
)
VSCode Version: 1.41.1
OS Version:Ubuntu 18.04
Steps to Reproduce:
# tree:
.
├── demo1
│ ├── __init__.py
│ └── test.py
├── __init__.py
├── auto.py
# auto.py
def func():
print("1")
# test.py
from auto import func
func()
Use examples to solve problems that arise in a project
Run the test.py file, and I get "ModuleNotFoundError: No module named 'func'"
I used 'CTRL '+ left mouse button in test.py to jump to func
The same code can be run in pycharm
If you run test.py directly then you need to add the parent folder to PYTHONPATH. Try:
import sys
sys.path.append("..\<parent_folder>")
from auto import func
Otherwise, if you merely want to import test.py in another .py file, you can use relative import of python
from . import auto #another dot '.' to go up two packages
auto.func()
Reference
Add this in test.py, before import:
import sys
sys.path.insert(0, "/path/to/project/root/directory")
For me it's not a good file organization. A better practice might be as below:
Let your project file tree be like:
.
├── __init__.py
├── lib
│ ├── auto.py
│ └── __init__.py
└── test.py
And write test.py like:
from lib.auto import func
func()
Simple one-line solution
from ... import auto
and call the function by using auto.func().
Using Python 3.6
I did create multiple modules (like DBmanager or jsonParser etc which I use across multiple different python projects)
For simplicity: I have created a module, lets call it 'gitmodule03'.
Internally it is supposed to be using yet another module from github 'gitmodule01' for parsing data. I have added 'gitmodule01' to 'gitmodule03' by
'git submodule add http://git/gitmodule01'
Separatenly, I am developing my 'MainPackage' which will use directly 'gitmodule03' and 'gitmodule01' (among others). I've added them all to my main Program with
'git submodule add http://git/gitmodule01'
'git submodule add http://git/gitmodule02'
'git submodule add http://git/gitmodule03'
and my package looks like this:
.
└── MainPackage
├── modules
│ ├── __init__.py
│ ├── gitmodule01
│ │ ├── __init__.py
│ │ └── mymodule01.py
│ ├── gitmodule02
│ │ ├── __init__.py
│ │ └── mymodule02.py
│ ├── gitmodule03
│ │ ├── __init__.py
│ │ ├── mymodule03.py
│ │ └── gitmodule01
│ │ └──
│ └── mymodule04.py
└── myMainProgram.py
At this moment 'gitmodule03' is NOT importing 'gitmodule01' internally. I was hoping that importing it in main myMainProgram.py would propagate across submodules (which is not the case)
If my myMainProgram.py imports them all:
from modules.gitmodule01.mymodule01 import my01class
from modules.gitmodule02.mymodule02 import my02class
from modules.gitmodule03.mymodule03 import my03class
my03class() # will work
my02class() # is internally using 'my03class()' and will error out:
NameError: name 'my03class' is not defined
How can I design those so they can work independently as well as within bigger package, in clean, pythonic way ?
I would like to have those modules idependent so they won't have to use any hard coded sys.path() methods
Edit Test Cases:
1.
myMainProgram.py
sys.path.insert(0, "modules/gitmodule03/gitmodule01/")
from mymodule01 import my01class
from modules.gitmodule03.mymodule03 import my03class
my01class() #works
my03class() # NameError: name 'my01class' is not defined
2.
myMainProgram.py
from modules.gitmodule03.gitmodule01.mymodule01 import my01class
from modules.gitmodule03.mymodule03 import my03class
my01class() #works
my03class() # NameError: name 'my01class' is not defined
3.
mymodule03.py
from gitmodule01.mymodule01 import my01class
my01class() #works
myMainProgram.py
from modules.gitmodule01.mymodule01 import my01class
from modules.gitmodule03.mymodule03 import my03class
my03class() # ModuleNotFoundError: No module named 'gitmodule01'
4.
mymodule03.py
from .gitmodule01.mymodule01 import my01class
my01class() # ModuleNotFoundError: No module named '__main__.gitmodule01'; '__main__' is not a package
myMainProgram.py
from modules.gitmodule03.mymodule03 import my03class
my03class() # works
With Test Case #4 It looks like i could make myMainProgram.py work but i would have to break module on its own.
So far I could not find better option to have both working myMainProgram.py and mymodule03.py on its own.
At the moment I am checking name variable to see whenever module is working on its own or whenever it is run from other package:
mymodule03.py
if __name__ == '__main__':
from gitmodule01.mymodule01 import my01class
my01class() # works
else:
from .gitmodule01.mymodule01 import my01class
myMainProgram.py
from modules.gitmodule03.mymodule03 import my03class
my03class() # works