I made a module that makes a linear regression model and draws a graph.
So the module needs to import some packages such as sklearn and matplotlib.
And I want to import this module to another python file and use it.
I think either of the two python files needs to import the above packages..
which of them needs to import?
In below case, my_module.py should import LinearRegression? or my_module2.py should?
ex)
my_module.py
---------------------------
**from sklearn.linear_model import LinearRegression**?
class myclass:
def a (self):
lr = LinearRegression()
my_module2.py
------------------------------
**from sklearn.linear_model import LinearRegression**?
from my_module import myclass
i = myclass()
i.a()
First you import it in my_module.py, then import * from my_module.py to my_module2.py.
my_module.py
---------------------------
from sklearn.linear_model import LinearRegression
class myclass:
def a (self):
lr = LinearRegression()
my_module2.py
------------------------------
from my_module import *
i = myclass()
i.a()
Related
I have multiple python scripts, like this:
utils.py
module1.py
module2.py
module3.py
main.py
...
In utils.py:
import some_lib
role_id = "some_key_1"
secret_id = "some_key_2"
def initialize():
real_key = some_web_request(role_id, secret_id)
some_lib.init(real_key)
# define some other functions using some_lib
In module1.py, module2.py, module3.py:
import utils
# define some other functions using functions in utils.py
In main.py:
import module1
import module2
import module3
# do something
I want to run utils.initialize() only once for initializing some_lib. However, due to how import works in python, if I put initialize() on global area in utils.py then it will run on every import statement run.
I can implement this like this, in utils.py
import some_lib
initialized = False
def initialize():
# same as above
if not initialized:
initialize()
initialized = True
Is this a good practice? Is there better or elegant way to implement this?
In MicroPython I created two modules 'mod_a' and 'mod_b'.
I am trying to grab functionality from one to the other and the other way around.
|mod_a
| | foo.py
| | __init__.py
|mod b
| | baa.py
| | __init__.py
foo.py
# necessary to grab module mod_b
import sys
sys.path.append('.')
from mod_b import Baa
class Foo:
b = Baa()
b.printer()
def drinker(self):
print('Drinking')
baa.py
import sys
# not working
# from mod_a import Foo
class Baa:
def printer(self):
print('Printer')
print('b.Baa', sys.path) => ['.' ...]
# ==> how to get this working
# a = Foo()
# a.drinker()
So far I tried
import sys
import os
if '/' not in sys.path:
sys.path.insert(0, os.getcwd())
sys.path.insert(1, '.')
sys.path.insert(2, '/mod_b')
sys.path.insert(2, '/mod_a')
sys.path.insert(2, '.mod_b')
sys.path.insert(2, '.mod_a')
and
sys.modules.get('.mod_b')
Please note that the following code only solves the import problem.
The code still has a circular import problem.
ImportError: cannot import name 'Baa' from partially initialized
module 'mod_b.baa' (most likely due to a circular import)
(C:\Users\Guest\test\.\mod_b\baa.py)
When initializing class Foo, it needs to call Baa.printer() which calls a = Foo() need the uninitialized class Foo, you need to fix that later.
foo.py
import sys
sys.path.insert(0,'.')
from mod_b.baa import Baa
class Foo:
b = Baa()
b.printer()
def drinker(self):
print('Drinking')
baa.py
import sys
sys.path.insert(0,'.')
from mod_a.foo import Foo
class Baa:
def printer(self):
print('Printer')
a = Foo()
a.drinker()
Circular imports are not supported in MicroPython up to v1.18.
MicroPython is largely based on Python 3.4 and differences to Python 3.5 are documented including circular imports.
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.
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.
I have a program that is laid out like the following:
test\test.py
test\modules\base.py
test\modules\blah.py
I need to load modules by name. Each module implements a class with the same methods, so I load them into a dictionary so that I can reference them as needed. I'm getting the follow error trying to do a relative import.
File "modules/blah.py", line 1, in <module>
from .base import BaseModule
ImportError: attempted relative import with no known parent package
Is there a way to use relative imports from code imported using importlib?
I'm using Python 3. The following is a simple example showing this error...
test\test.py:
#!/usr/bin/env python
import importlib
class Test():
def __init__(self):
spec = importlib.util.spec_from_file_location("blah", "modules/blah.py")
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
def main():
t = Test()
if __name__ == '__main__':
main()
test\modules\base.py:
class BaseModule():
modulename = "base"
def __init__(self,name):
print("Initializing module %s" % (self.modulename))
test\modules\blah.py:
from .base import BaseModule
class BlahModule(BaseModule):
modulename = "blah"
Adding the following code should help:
import os
import sys
module_path = "modules/blah.py"
module_dir = os.path.dirname(module_path)
if module_dir not in sys.path:
sys.path.append(module_dir)
# do actual import of module here