python not able to import - python

Importing inside the function, Python is not able to import this. Outside the function, it works fine. This is actually a Django server and view_page function gets called from outside the file.
So, basically doing this throws up an error "Error : No module named plotting"
My code:
def view_page():
from bokeh.plotting import figure # This throws up an error
pass
This works absolutely fine;
from bokeh.plotting import figure
def view_page():
pass

Related

Import My Own Function into Azure Timer Trigger

Picture of Code
If you look at my code you can see I am trying to use my created function, send(message), to send a message. However, I am unsure how to import my file, which you can see on the left, SMSpy. Can anyone tell me how I can properly import my file, Thanks!
If SMSpy has a class use
One time call:
Call it once with an instant object created.
import SMSpy
SMSPy.<classname>().send()
Multiple Calls:
Create an object and call
import SMSpy
Obj = SMSPy.<classname>()
Obj.send()
If no class:
import SMSpy
SMSpy.send()

Jupyter Import Module AND use function. How can I do that?

I need to import a function classify(par1, par2, par3)
from a module called _Y03_Labeling. Importing does work, but using its functions with more than one extra parameter doesnt work.
Question : How can I import functions with more than one Parameter?
.
What I already tried ( Without Success):
I successfully can run the whole notebook2 from notebook1 with the following code:
import _Y03_Labeling
Labeling =_Y03_Labeling
(Why I know if it is successful? Because its comments are printed out). Whenever I try to run:
X,y = classify(a,b,c)
I get the following error: "TypeError: 'module' object is not callable"
I tried many variations from the import line, including:
import _Y03_Labeling
Labeling =_Y03_Labeling
X,y = Labeling.classify(a,b,c)
# or:
from _Y03_Labeling import classify
# or:
import _Y03_Labeling
X,y = _Y03_Labeling .classify(a,b,c)
Sadly none of them worked for me.
Things i also did so far:
shutting down the _Y03_Labeling notebook before I run the main Notebook
putting the function in the second notebook into a class, importing the class from the notebook and calling the function. (works only if function needs 1 parameter)
also i didnt forgot "self" i the function declaration with the class try.
I am glad, this forum exists and thankful for every possible help.

Cannot import function from another file

I have a file called hotel_helper.py from which I want to import a function called demo1, but I am unable to import it.
My hotel_helper.py file:
def demo1():
print('\n\n trying to import this function ')
My other file:
from hotel.helpers.hotel_helper import demo1
demo1()
but I get:
ImportError: cannot import name 'demo1' from 'hotel.helpers.hotel_helper'
When I import using from hotel.helpers.hotel_helper import * instead of from hotel.helpers.hotel_helper import demo1 it works and the function gets called. I tried importing the whole file with from hotel.helpers import hotel_helper and then call the function with hotel_helper.demo1() and it works fine. I don't understand what's wrong in first method. I want to directly import function rather using * or importing the whole file.
If you filename is hotel_helper.py you have to options how to import demo1:
You can import the whole module hotel_helper as and then call your func:
import hotel_helper as hh
hh.demo1()
You can import only function demo1 from module as:
from hote_helpers import demo1
demo1()
From your fileName import your function
from hotel.helpers import demo1
demo1()
You can import a py file with the following statement:
# Other import
import os
import sys
if './hotel' not in sys.path:
sys.path.insert(0, './hotel')
from hotel import *
NOTE:
For IDE like PyCharm, you can specify the import path using the Project Structure setting tab (CTRL+ALT+S)
Helpful stack overflow questions [maybe off topic]:
What is the right way to create project structure in pycharm?
Manage import with PyCharm documentation:
https://www.jetbrains.com/help/pycharm/configuring-project-structure.html
This is probably a duplicate of: https://stackoverflow.com/posts/57944151/edit
I created two files (defdemo.py and rundefdemo.py) from your posted 2 files and substituted 'defdemo' for 'hotel.helpers.hotel_helper' in the code. My 2 files are in my script directory for Python 3.7 on windows 10 and my script directory is in the python path file python37._pth. It worked.
defdemo.py
def demo1():
print('\n\n trying to import this function ')
rundefdemo.py
from defdemo import demo1
demo1()
output
trying to import this function
I was able to solve the issue, it was related to some imports I was making in my file, when I removed all the import statement in my hotel_helper.py ,the code started working as expected , Still I don't understand reason why the issue was occurring. anyway it works.
This ImportError can also arise when the function being imported is already defined somewhere else in the main script (i.e. calling script) or notebook, or when it is defined in a separate dependency (i.e. another module). This happens most often during development, when the developer forgets to comment out or delete the function definition in the body of a main file or nb after moving it to a module.
Make sure there are no other versions of the function in your development environment and dependencies.

I got the message: "Bad import syntax:" when I try to import a python function inside my gnuradio flowgraph

I'm trying to do this tutorial on gnuradio page:
https://wiki.gnuradio.org/index.php/TutorialPythonFunctions
It looks to be simple but I got the message "bad import syntax" when I try to import my function "testpy".
Has someone had the same problem or knows how to solve it?
This is unfortunately cryptic. What you have to write in the box is a Python import statement,
not just the name of the module to import. That is, write
import testpy
instead of just testpy.
You can also use from <module> import <name>, <name> and all the other forms of the import statement. (That's why it wants the statement and not just module names: so you can do either kind of import.)

web2py python - ImportError cannot import name

I keep getting this error: <type 'exceptions.ImportError'> cannot import name get_cert_infos.
I'm pretty sure I'm importing everything correctly. The file with the problem is participant.py and has:
from datetime import date
from widgets import SelectOrAdd, LabelSortedOptionsWidget, DynamicSelect, \
AutocompleteReferenceWidget
from communication import handle_notification, send_email
from utility import create_temp_password, hash_password
from export import get_cert_infos, build_certificate
I have exports.py and the get_cert_infos and build_certificate methods do exist inside of there. I don't understand what the problem is.
I looked at several of the other posts on this and they all seem to be saying that this is most likely a circular import problem
I have export installed and updated export==0.1.2
ImportError: Cannot import name X
Try double checking the spelling, I know it's dumb, but it happens.
if it's not that, try writing this method in export
def hello_world():
print 'hello world'
then
import export
export.hello_world()
if that works, it may be something wrong with the method itself, if it doesn't I imagine that the name export is reserved and is causing conflicts (my code editor doesn't mark it as reserved tho).
is it necessary to import only those two methods? or could you import the whole module and use the needed methods as in the case of the hello_world? does that cause you troubles? if you delete get_cert_infos does build_certificate give you any troubles?

Categories

Resources