I'm trying to import external modules into my controller code for a web2py application. The module I'm importing is located in myapp/modules and seems to import fine. However, it doesn't let me call any of the functions defined in there; gives the following error
'module' object has no attribute 'testfunc'
I'm importing like this:
import json_utils as u
And calling like this:
u.testfunc()
Am I missing something obvious here? I have tried stop/starting the server in case its not reloaded the file.
Cheers
EDIT: Here is the entire json_utils.py file:
def testfunc():
return 3
def testfunc2():
# some stuff
return 5
Problem is web2py caching external modules. Stop/Starting the server is not enough, I need to kill the whole thing are reboot.
It's saying that json_utils has no function called testfunc
The module json_utils has no built-in function testfunc()
for example if I do
import random
u.nonfunction()
and then I run it I get
AttributeError: 'module' object has no attribute 'nonfunction'
but if I do a function that it has
import random
random = u.randrange(1,10)
print(random)
It works properly
Related
I have a Jupyter Notebook - we'll call it main, and I'm running it piecemeal in Jupyter Lab 3.0.14 using a Python 3 environment.
Within this notebook, I'm trying to import custom functions from a module, we'll call this one module.py, which is in the same directory as main.
It looks something like this:
//main
import sys
import os
from module import foo, bar
//module.py
def foo():
return
def bar():
return
When I run the import statements in main, it throws an error
ImportError: cannot import name 'bar' from 'module' (/module.py)
If I run
from module import foo
or
from module import *
It's fine (although calling the functions will throw an error that the function is not defined). In fact, it will import foo even if there's no function named foo in module.py. It won't import any of my own functions by name. I feel like I must be missing something fundamental, here...
Any help would be greatly appreciated!!
Okay, this is my first time using Jupyter Lab, and this problem came from a fundamental misunderstanding of how Jupyter (and maybe other IDEs?) manages packages.
In short, as I understand it, re-running the code which imported functions from module.py did not actually re-import the contents. The only things it imported were what I asked for the first time, and any subsequent calls to import functions only referenced that first static image I pulled.
Therefore, if module.py had one function, foo, and I ran:
from module import foo
...then made some changes, such as editing the contents of foo and adding bar, running:
from module import foo, bar
...didn't actually do anything but reference that first copy of module.py I pulled. Because bar didn't exist the first time I ran the import function, it "couldn't see it", no matter how many times I re-ran the import function. I had to restart the kernel to fix it, basically.
I noticed this because when I changed the name of the script, it would import the functions that weren't previously working, but then edits to the functions, even once imported, weren't recognized.
This link provided the clue I needed to figure this out.
Hopefully this helps somebody!
Restart the kernel in the Jupiter notebook. I solved a similar import name issue by restarting the kernel.
I have two files: 'my_main.py' and 'my_functions.py'. I wrote two functions in 'my_functions.py', 'first_function' and 'second_function' and imported this module into 'my_main.py'. However, I then added a third function into 'my_functions.py' called 'third_function':
def first_function():
print("hello1")
def second_function():
print("hello2")
def third_function():
print("hello3")
I found that I could not import 'third_function' into 'my_main.py', getting an error - AttributeError: module 'my_functions' has no attribute 'third_function'.
To understand this, in 'my_main.py' I then ran:
import my_functions
help(my_functions)
and had as my output:
Help on module my_functions:
NAME
my_functions
FUNCTIONS
first_function()
second_function()
For some reason 'my_functions.py' would not recognise any more functions after the initial two, meaning I couldn't then import 'third_function' into 'my_main.py'. How can I fix this?
Make sure you have saved your file my_functions.py before running my_main.py. Unless the file is saved, the update (addition of third_function) won't be recognized. Have been doing this for years, and still make this mistake constantly. Editing not enough. Need to edit and save.
You need to explicitly reload the module if you've made changes since the last import. There is a built-in function for this
You can reload your module as follows:
import importlib
importlib.reload(my_functions)
See this answer for a much more detailed explanation.
I have already browsed all the topics close to this but couldn't find the answer to my question.
I have the following directory structure
--Codes
+------mod_welch.py
+------__init__.py
+------Toy_Model
++---------__init__.py
++---------linear_filter.py
Both of __init__.py's are empty and I am trying to access to mod_welch.py in the body of linear_filter.py with no success. When I want to use realtive access to upper folders as
from ..mod_welch import welch where welch is a function inmod_welch I receive:
ValueError: Attempted relative import in non-package
What am I doing wrong?
In genereal, your module should be import-ed somewhere and then used. Error message that you're receiving suggests that there is unusual execution method of module code. I suspect that youre testing your code by runnig python linear_filter.py (it gives the same error). Thats not the way.
As I said above you need to actually use your module if you want to test its functionality. To do that, you can import your module in another module or main script, for example:
from Codes.Toy_Model.linear_filter import some_method
if __name__ == '__main__':
print(some_method())
Where some_method is defined in linear_filter.py as:
from Codes.mod_welch import welch
def some_method()
welch()
If that's not the case then please provide additional information about what is an usage (execution) method of your module -- which script are you running and how. Any sys.path.append magic etc. are important here too.
I am trying to connect Modelica and Python using the Python27 block, provided by the Berkeley Simulations Lab:
http://simulationresearch.lbl.gov/modelica
I use this block to call a Python function:
def Test2(WriteValues):
''' Connection Test - Works if started from Dymola
'''
#Doing nothing and returning the input
ReturnList=WriteValues
return (ReturnList)
works perfectly.
Now I need to import some modules
#Importing Python modules works in general
import sys
import thread
import time
works aswell
Only now I want to import a module that is not part of Python but a site-package:
def Test1(WriteValues):
'''Connection Test - Doesnt work if started from Dymola
'''
#Importing some Bacpypes Module
#Path is by default C:\Python27\Lib\site-packages\BACpypes-0.7-py2.7.egg\bacpypes
#My os is win7
from bacpypes.core import run
#Doing nothing and returning the input
ReturnList=WriteValues
return (ReturnList)
This does not work. It does not matter if I import the BACpypes module inside a function or globally - the error is always
'module' object has no attribute 'argv'
Colleagues pointed me to the idea that it might be related to a multiple import problem. The function is being called by Modelica every 10 seconds (real-time-simualtion).
If I call the function Test1 outside of Modelica, there is no problem. It only fails using the Python27 block!
Does anyone have an idea about how to make the BACpypes import work?
UPDATE 2013-10-16:
I printed out the value of sys.argv for the script excecution in the Python directory and an excecution from Modelica.
sys.argv from Python directory:
['C:\\Python27\\Testcon.py']
sys.argv if function is called from inside Modelica:
['modpython']
Might this in any way be related to the error message I get?
The bug is caused because bacpypes uses sys.argv but the Python interpreter did not call PySys_SetArgv.
This will be fixed in the next version of the Modelica Buildings library, see https://github.com/lbl-srg/modelica-buildings/issues/191
I first tried with the interpreter to produce uuid's with python's uuid module. I did the following:
>>>import uuid
>>>uuid.uuid1()
UUID('d8904cf8-48ea-11e0-ac43-109add570b60')
So far so good. I create a simple little function to produce the uuid's.
import uuid
def get_guid():
return uuid.uuid1()
if __name__ == '__main__':
print get_guid()
and I get the following error:
AttributeError: 'module' object has no attribute 'uuid1'
Ok...hmm...go back to the interpreter and now it too is broken. I get the same error running the same code I used to test this. I am baffled. What makes uuid break like this? And what is wrong with my code?
I am using python 2.6
Your test file name is most likely named uuid.py
When you went back to the interpreter, you launched the interpreter from the same directory, which by default, will first look for the module name to import in your current working directory.
Just change your test file name to something else, i.e. uuid_test_snippet.py