I have a file like this:
app.py:
def app():
........
test.py:
from app import app
def test():
......
but when I try to import " test" function in app.py file I am getting " Module error"
how can I solve this circular imports error?
Try doing a simple import, it worked without any Module error:
import app
def test():
print('b')
app.app()
If the problem persist maybe is because is not finding the module in the directory, try on adding it in the sys.path:
import sys
sys.path.insert(1, '/path/to/module')
Related
I'm trying to make a Flask api :
app.py
routes
-> __init__.py
-> myobject.py
# app.py
from flask import Flask
from flask_restful import Api
from routes import MyObject
app = Flask(__name__)
api = Api(app)
api.add_resource(MyObject, '/')
if __name__ == '__main__':
app.run()
# __init__.py
from myobject import MyObject
# myobject.py
from flask_restful import Resource
class MyObject(Resource):
def get(self):
return {'hello': 'world'}
When I run my application (python app.py), I get a ModuleNotFoundError: No module named 'myobject'
I don't understand why python can't find my module myobject. Is there something i'm missing in my __init__.py
For __init__.py, it also needs the relative import or absolute import path in order to work correctly.
# __init__.py
# relative import
from .myobject import MyObject
# absolute import
from routes.myobject import MyObject
In another approach, you can write to import MyObject more specifically like this and leave __init__.py an empty file.
# app.py
# ...
from routes.myobject import MyObject
# ...
I believe the problem is due to your app running in the "main" directory and your import residing in a sub directory. Basically your app thinks you are trying to import "/main/myobject.py" instead of "/main/routes/myobject.py"
Change your import in __init__ to from .myobject import MyObject
The . means "this directory" (essentially).
I have the following files in my directory:
`directory/
__init__.py
GUI.py
Data.py`
file GUI.py looks like this:
import os
import tkinter as Tk
from .Data import data
class GUI(object):
def __init__(self):
do things ...
file Data.py looks like this:
import os
class data(object):
do things ...
class data2(object):
do other things ...
I tried to run the GUI.py but get the following error for the from .Data import data
ERROR: SystemError: Parent module '' not loaded, cannot perform relative import
I use the import as it written in the relative import documentation. Why doesnt it work?
The following should work:
from Data import data
I can not import functions from other files to __init__.py in a flask. Importing something from a file gets an error 500.
__init__.py
from flask import Flask
from fel import fel
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(debug=True)
fel.py
def fel(a,b):
c = a+b
return (c)
If I delete the following line in the __init__.py file
from fel import fel
Everything is OK.
__init__.py and fel.py are in the same directory
I am working in Python 3.4
Where is the mistake?
edit:
structures
FlaskApp\
__init__.py
fel.py
use relative import
from .fel import fel
fel(something)
Explanation:
The problem of import fel is that you don't know whether its an
absolute import or a relative import. fel could a module in python's
path, or a package in the current module.
Source https://softwareengineering.stackexchange.com/questions/159503/whats-wrong-with-relative-imports-in-python
Your import should be:
from FlaskApp.fel import fel
And the parent directory of FlaskApp needs to be present in your sys.path somehow (for example, set the PYTHONPATH environment variable).
just
from flask import Flask
from .fel import fel
app = Flask(__name__)
#app.route('/')
def hello_world():
number = fel(4,6)
return (number)
if __name__ == '__main__':
app.run(debug=True)
I'm a newbie in using Python.
What I need is simple: import a module dynamically.
Here is my little test:
#module
class Test:
def func(self, id, name):
print("Your ID: " + str(id) + ". Your name: " + name)
return
I put this class in a file named my_module.py and the path of the file is: c:\doc\my_module.py.
Now I create a new python project to import the file above.
Here is what I do:
import sys
module = sys.path.append(r'c:\doc\my_module.py')
myClass = module.__class__
print(myClass)
However, I got this result:
<class 'NoneType'>
Why can't I get Test?
Is it because the way to import a module is wrong or because I need to do some config to import a module?
You're doing it wrong. Here's how you should import your module with sys.path.append:
import sys # import sys
sys.path.append(r'c:\doc\') # add your module path to ``sys.path``
import my_module # now import your module using filename without .py extension
myClass = my_module.Test # this is how you use the ``Test`` class form your module
try this:
import sys
sys.path.append(r'c:\doc\') # make sure python find your module
import my_module # import your module
t = my_module.Test() # Initialize the Test class
t.func(1, 'test') # call the method
The way to import a module is through the import command. You can specify the path with sys as a directory. You also need to instantiate the class within the module (through the my_module.Test()). See the following:
import sys
sys.path.append(r'c:\doc\\')
import my_module
myObj = my_module.Test()
myClass = myObj.__class__
print(myClass)
Here is my directory structure:
In file keyword.py I import lottery.lottery at the first line like this:
from lottery.lotterya import Lottery
In file rule.py I import lottery.keyword dynamically like this:
__import('lottery.keyword') but it reports an error "No module named lotterya".
I don't know what to do. Can anyone help?
I dynamically import a module
Here is one solution for your question. It uses importlib to do dynamic import.
In ruly.py
import importlib
if __name__ == '__main__':
mKey = importlib.import_module('lottery.keyword')
MyKeyword = getattr(mKey,'MyKeyword')
k = MyKeyword()
k.mPrint()
In keyword.py
from lottery.lotterya import Lotterya
class MyKeyword():
def __init__(self):
pass
def mPrint(self):
print 'Hello, keyword'
l = Lotterya()
l.lPrint()
In lotterya.py
class Lotterya:
def __init__(self):
pass
def lPrint(self):
print 'Hello, Lotterya'