ModuleNotFoundError: No module named 'scripts.custom_timer' - python

I pulled code from another team member whose code base works without errors, but anytime I try to run this I get this error. I tried renaming the class and importing in different ways but nothing's been working. Here's the following main code.
import sys
from PySide6.QtCore import *
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCharts import *
from database.db import MySql
from package.main_chart import StockChart
from package.indicator_charts import IndicatorCharts
from scripts.custom_timer import RepeatedTimer
here is the current file structure
Why is RepeatedTimer not being detected?

Ok, if it is in custom_timer.py then use
from scripts.custom_timer import RepeatedTimer
You can have a look at https://docs.python.org/3/reference/import.html#submodules

Related

Importing a deck in anki python

I am working on an addon, for anki, which requires me to import a new deck using its python API. I found the function called test_apkg which seems to have parts which do that. I tried implimenting that below, I am getting no errors, however the deck does not get imported. How do I make my testFunction() import a deck for anki?
from aqt import mw
from aqt.utils import showInfo, qconnect
from aqt.qt import *
from anki.importing import *
import os
from anki.collection import Collection as aopen
#from anki.pylib.tests.shared import getEmptyCol
from .shared import getEmptyCol
def testFunction() -> None:
file = "flashcards.apkg"
col = getEmptyCol()
apkg = r"path_to_file/flashcards.apkg"
imp = AnkiPackageImporter(col, apkg)
imp.run()
# create a new menu item, "test"
action = QAction("Import", mw)
# set it to call testFunction when it's clicked
qconnect(action.triggered, testFunction)
# and add it to the tools menu
mw.form.menuTools.addAction(action)

Can't import helper functions from module - "cannot import name ..." error

I have two scripts, a main file and a smaller module storing some helper functions. My main script opens with
from collections import namedtuple
import rospy
import serial
from sensor_msgs.msg import Imu, MagneticField
from quaternions import euler_deg2rad, euler2quat_ZYX
and in quaternions.py, I have
from collections import namedtuple
import numpy as np
def euler_deg2rad(yaw, pitch, roll):
....
Attempting to run the main script throws an ImportError: cannot import name 'euler_deg2rad' from 'quaternions'
What's going on here? Is importing namedtuple twice creating a circular dependency? Replacing the 'from' import in the main file with just import quaternions works fine, but I'd rather not import the whole module.

NameError: name 'threading' is not defined will running Python Program in Terminal

I have imported the following modules in my python file:-
from socket import *
from threading import *
But, I am still getting this error:-
NameError: name 'threading' is not defined
It is showing the error on this line:-
receive_thread = threading.Thread(target=receive)
Could anyone please let me know how could I resolve this issue? Any help would be highly appreciated! Thanks:)
If you import from threading import * you have to access the methods directly, without calling the class threading before. So you just import every method from threading and just use them:
from threading import *
receive_thread = Thread(target=receive)
otherwise import and then refer to the module itself
import threading
receive_thread = threading.Thread(target=receive)
import socket programming library
import socket
# import thread module
from _thread import *
import threading
print_lock = threading.Lock()

Python Class Initialization Error. Cannot initialize an Imported class

I am trying to initialize a class ModuleLMPC from commander.py which is the main class. I get following error.
File "D:\CARLA\PythonAPI\examples\fnc\commander.py", line 89, in __init__
self.LMPC = ModuleLMPC(self.dt, self.LapTime, self.maxLaps, self.Map)
TypeError: 'module' object is not callable
I import ModuleLMPC as:
from ModuleLMPC import ModuleLMPC
I am trying to import ModuleLMPC from ModuleLMPC.py file. They have the same name.
The ModuleLMPC first few lines look like:
from SysModel import Simulator, PID
from Classes import ClosedLoopData, LMPCprediction
from PathFollowingLTVMPC import PathFollowingLTV_MPC
from PathFollowingLTIMPC import PathFollowingLTI_MPC
from LMPC import ControllerLMPC
from Utilities import Regression
import numpy as np
import pdb
import pickle
class ModuleLMPC(object):
def __init(self, dt, Time, maxlaps, map):
# ======================================================================================================================
# ============================ Choose which controller to run ==========================================================
# ======================================================================================================================
self.RunPID = 1
self.RunMPC = 1
And I initialize ModuleLMPC in commander.py as:
self.LMPC = ModuleLMPC(self.dt, self.LapTime, self.maxLaps, self.Map)
I even moved everything in the same folder. Not sure what the error means.

How to mock in a python unittest a library not installed locally?

I am trying to test using unittest a python 3.6 script that starts with (my_app.py):
import sys
from awsglue.utils import getResolvedOptions
args = getResolvedOptions(sys.argv, ['opt1', 'opt2', 'opt3'])
opt1 = args['opt1']
opt2 = args['opt2']
opt3 = args['opt3']
....
so in my test I did something like:
import unittest
import datetime
from mock import patch
import my_app
class TestMyApp(unittest.TestCase):
#patch('awsglue.utils.getResolvedOptions')
def test_mock_stubs(self, patch_opts):
patch_opts.return_value = {}
....
but the test soon fails at import my_app with:
ModuleNotFoundError: No module named 'awsglue'
as there is not awsglue locally installed. How can I test a module that import a not locally installed library and also mock it in my test?
You'll need to mock the imported module before the import for my_app happens. patch won't work here, because patch imports the module in order to patch it. And in this case, that import itself would cause an error.
To do this the simplest way is to trick python into thinking that awsglue is already imported. You can do that by putting your mock directly in the sys.modules dictionary. After this you can do the my_app import.
import unittest
import sys
from unittest import mock
class TestMyApp(unittest.TestCase):
def test_mock_stubs(self):
# mock the module
mocked_awsglue = mock.MagicMock()
# mock the import by hacking sys.modules
sys.modules['awsglue.utils'] = mocked_awsglue
mocked_awsglue.getResolvedOptions.return_value = {}
# move the import here to make sure that the mocks are setup before it's imported
import my_app
You can move the import hack part to a setup fixture method.
However I would suggest just installing the package. The import hack can be very difficult to maintain.

Categories

Resources