Hi I'm having trouble importing classes and variables into python files from other python files. Functions work fine.
As a test, I set up file1 with a function, a class, an assigned instance of the class, and a random variable. I then used various methods in file2:
1.
import file1
2.
from file1 import *
error: 'name not defined'
3.
from file1 import variable,class,instance,etc
error: cannot import name Class
4+. And then doing some other things...
creating a init.py file
or then trying to set directory:
import os
os.chdir("/Users/mardersteina/Documents")
Not sure what I'm doing wrong. Function imports fine, but can't figure this one out with the classes and variables no matter what I'm looking up.
Untitled7:
def happy():
print "yo!"
class Tap(object):
def __init__(self,level):
self.level = level
level4 = Tap(4)
x = 14
Untitled9:
%run "/Users/mardersteina/Documents/Untitled9.py"
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/Users/mardersteina/Documents/Untitled9.py in <module>()
1 import Untitled7
2
----> 3 print Untitled7.x
4 """
5 from Untitled7 import Tap
AttributeError: 'module' object has no attribute 'x'
%run "/Users/mardersteina/Documents/Untitled9.py"
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/Users/mardersteina/Documents/Untitled9.py in <module>()
4 print Untitled7.x
5 """
----> 6 from Untitled7 import Tap
7
8 print Tap(4).level
ImportError: cannot import name Tap
%run "/Users/mardersteina/Documents/Untitled9.py"
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/Users/mardersteina/Documents/Untitled9.py in <module>()
11 from Untitled7 import *
12
---> 13 print level4.level
NameError: name 'level4' is not defined
I can see that you are running the file from an open console .
Most probably the issue is that you had imported the Untitled7.py previously when it only had one function . When you do that Python caches the module in sys.modules .
So if you try to import it in same session again, you would get the cached version from sys.modules , and that would be the reason any changes to the Untitled7 you did after importing it once are not visible.
To fix this issue, you can reload the module -
In Python 3.x , use importlib.reload() to reload the module (to take in new changes) , Example -
import importlib
importlib.reload(Untitled7)
In Python 2.x , use reload() method -
reload(Untitled7)
Or you can also close the python terminal and reopen it, and it should fix the issue.
Related
Problem
I would like to import a script containing many functions and then run them, so that I can use the function. I may have misunderstood the purpose of import. I am working in Jupyter.
Reprex
#Create the script in a local folder
%%writefile test.py
c = 500
def addup(a,b,c):
return a*b + (c)
#import the file and use it
import test
addup(1,5,c)
#Error message
---------------------------------------------------------------------------
# NameError Traceback (most recent call last)
# <ipython-input-1-71cb0c70c39d> in <module>
# 1 import test
# ----> 2 addup(1,5,c)
# NameError: name 'addup' is not defined
Any help appreciated.
You have not called the function! You need a dot . to call a function from a module.
This is the correct syntax:
import test
result = test.addup(1,5,c)
Import a specific function:
from test import addup
addup(1,5,c)
Importing all of the module's functions:
from test import *
addup(1,5,c) # this way you can use any function from test.py without the need to put a dot
I was trying to run this code but it says
NameError Traceback (most recent call last)
<ipython-input-5-49e303967177> in <module>
3
4 #Detector object created
----> 5 fd=FaceDetector(frontal_cascade_path)
NameError: name 'FaceDetector' is not defined
This is the code: -
#Frontal face of haar cascade loaded
frontal_cascade_path="../input/haarcascade-frontal-faces/haarcascade_frontalface_default.xml"
#Detector object created
fd=FaceDetector(frontal_cascade_path)
Read this. You need to import the FaceDetector into your code:
from face_detector import FaceDetector
After installing thermo.chemical, I wanted to know how I can check,
first of all, which version is installed on my computer and second where the program is located (path).
What do I have to enter in my terminal.
I tried the following, but it does not work.
import ipywidgets as widgets
def clicked(arg):
print(thermo.__version__())
button_download = widgets.Button(description = 'Version', button_style = 'primary')
button_download.on_click(clicked)
display(button_download)
This error ist showed:
Traceback (most recent call last)
<ipython-input-51-8d76d70a85bb> in clicked(arg)
3
4 def clicked(arg):
----> 5 print(thermo.__version__)
6
7 button_download = widgets.Button(description = 'Version', button_style = 'primary')
NameError: name 'thermo' is not defined
Many modules have variable __version__ and all modules, submodules and scripts have __file__
import thermo
print(thermo.__version__)
print(thermo.__file__)
BTW:
You have to import thermo to have access to elements in thermo.
Using from thermo.chemical import Chemical you have access only to Chemical but not to thermo.
I can't load class I defined in another file. It is strange behavior that I CAN load function defined in the same file.
XXXX.py
def hoge():
print('hoge')
class YYYY:
def hoge(self):
print('hoge')
I try to import and run XXXX as follow:
import XXXX
XXXX.hoge()
XXXX.YYYY
Then, I encountered the error
hoge
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-76-cbd9d0cb0faf> in <module>
2 XXXX.hoge()
----> 3 XXXX.YYYY
AttributeError: module 'XXXX' has no attribute 'YYYY'
I use Python 3.6
I've solved this problem.
This error occurs on jupyter-notebook.
But it works on terminal.
I don't know why this happens on jupyter.
I am quite new to writing modules in Python.
I use Python 3.5
I have a script called describeToolbox.py that contain functions that I would like to be able to call, like this one:
#describeToolbox.py
import shelve
def getRawData(prefix):
shelfFile = shelve.open('data'+prefix)
df = shelfFile['data'+prefix]
shelfFile.close()
return df
This is meant to retrieve a dataFrame from a shelve file
In my console now, I write the following statements:
In [7]:import shelve
import describeToolbox as TB
In [8]:TB.getRawData('Myprefix')
Out [8]:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-67160af666cc> in <module>()
----> 1 TB.getRawData('Myprefix')
C:\Users\Math\Documents\Docs\Commos\Notebooks\describeToolbox.py in getRawData(prefix)
1 def getRawData(prefix):
----> 2 shelfFile = shelve.open('data'+prefix)
3 df = shelfFile['data'+prefix]
4 shelfFile.close()
5 return df
NameError: name 'shelve' is not defined
It gives me an error message saying that the module 'shelve', the dependency, is not defined.
Basically I dont know where is the correct place to import all the dependencies so that my function can load them when I want to import it.
I would like to write a depository of functions I use often in one module and call them when needed.
Thank you for your help!