I have a pycharm project with two .py files signal.py and moving_average.py.
signal.py looks something like:
class signal_class(object):
long_short = 0
underlying = ""
def abc(self,...):
and moving_average.py looks something like:
import signal
import stock_wrapper
import pandas as pd
import signal
class SMA(signal.signal_class): #Error throws here.
df = None
s_w = None
Which looks correct to me but when I try to run I get the following error:
class SMA(signal.signal_class):
AttributeError: 'module' object has no attribute 'signal_class'
The error is thrown from the line market above.
I thought I followed the tutorial quite closely but I am unsure what is causing this.
Thank you very much for anyone who can help on this.
Python has a builtin package named signal
Python2
Python3
So, when you do import signal that is being imported.
If you want to import your signal_class - either rename signal.py or do
from .signal import signal_class
and inherit SMA from there
Related
How to access models/usrs from resources/user
├───models
| └───user.py
├───resources
| └───user.py
First I import it like this one:
from code.models.user import UserModel
But I got a compile-time error:
`Cannot find reference 'models' in 'code.py'`
And I tried another way like this one:
from ..models.user import UserModel
But I got a runtime error:
ImportError: attempted relative import beyond top-level package
And I added init.py in both files but still doesn't work.
And also I tried these solutions but they don't fix my issue, please help me
Add the models and resources directories to your $PYTHONPATH or use sys.path:
export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add
Or:
import sys
sys.path.insert(0, "/home/project-name/models")
Or add "__ init__.py" to code directory:
from code.models import user
And finally, I fix my issue:
As #Little Bamboo said: I used sys concept for the import like below:
import sys
sys.path.append("D:\\Projects\\Python\\UdemyProjects\\SixSimplifyingTwo")
from mycode.models.userone import UserModel
But that was not all, some of my files had the same names and I changed the names of the files.
And also I had a folder called code, and from the below link I understood that code is a built-in Python module and I changed the code folder name.
Werkzeug AttributeError: 'module' object has no attribute 'InteractiveInterpreter'
I fetched a cristal structure of a protein using the function retrieve_pdb_file from Bio.PDB. The default format has changed from PDB to PDBx/mmCif. I want to extract the protein sequence from the header in the cif file. There is supposed to be a simple function in Bio.PDB called MMCIF2Dict to do this but the module is not callable. I also downloaded the cif file manually and put it in the script folder but still the same error. My biopython is up to date. Am I doing something wrong or the module is not well implemented? Thank you for your answers.
from Bio.PDB import *
cifFile = '1bu7.cif'
mmcif = MMCIF2Dict(cifFile)
TypeError: 'module' object is not callable
The module is well implemented. The problem with your code is that you are calling a module instead of a function. In your particular case the module and the function have the same names hence the confusion.
To solve that you need to fix your code as follows:
from Bio.PDB import *
cifFile = '1bu7.cif'
mmcif = MMCIF2Dict.MMCIF2Dict(cifFile)
Try:
from Bio.PDB.MMCIF2Dict import MMCIF2Dict
Instead of :
from Bio.PDB import *
So I am having trouble importing classes in the same directory and getting them to work properly.
I currently have the following hiearchy
BBDriver.py
bbsource:
BouncyBallEnv.py
Console.py
resources:
misc:
objects:
Ball.py
Platform.py
My problem is between the 2 files in the bbsource directory. I have figured out how to get access from the bbsource directory down to the classes in the objects directory and vice versa but when I try to from BouncyBallEnv import BouncyBallEnv in the Console class I get the following error:
File "E:\PycharmProjects\BouncyBallPythonV0\bbsource\Console.py", line 5, in
from BouncyBallENV import BouncyBallEnv
ImportError: cannot import name 'BouncyBallEnv'
I have tried several things like:
from bbsource import BouncyBallEnv
from bbsource.BouncyBallEnv import BouncyBallEnv
But I can't get it to work.
The only time I could get it to work is when I did the following:
import bbsource.BouncyBallEnv
#Extra
print(bbsource.BouncyBallEnv.BouncyBallEnv.WIDTH)
But there must be a better way to do it than that so that I wouldn't have to type that lengthy statement that is in the print statement every time that I want to use a static variable in BouncyBallEnv.
I am still quite confused on how the Python importing works so I'm not sure how to go about doing this. Thank you.
NOTE: Running Python 3.5.1
the thing you need is aliases :
import bbsource.BouncyBallEnv as bbe
#Extra
print(bbe.WIDTH)
and you can't import a module with the from ... import ... syntax. Only attributes. It work like this :
import <module> [as <alias>]
or
from <module> import <attribute> [, <attribute2>...] # import some attributes
from <module> import * # import everything
with the second one, you could have done :
from bbsource.BouncyBallEnv import WIDTH
# the variable WIDTH is directly loaded : watch out for collision !
print(WIDTH)
It is abosolue_import rule.
try
from .BouncyBallENV import BouncyBallEnv
to access module in relative position.
besides, there should be an __init__.py file under bbsource directory
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?
I'm trying to use cubehelix in python however I've been getting simple problems which I don't think should be showing up. The code I'm using is the following:
import matplotlib.pyplot as plt
import numpy as np
import cubehelix
cx1 = cubehelix.cmap(reverse=True)
alplot = plt.imshow(rtotal_rotated,vmax=1100,extent[-21,19,23,-17],cmap=cx1)
However the following error comes up when I run the code:
AttributeError: 'module' object has no attribute 'cmap'
I know this can't be right since I'm simply following the code from this tutorial
http://www.ifweassume.com/2014/04/cubehelix-colormap-for-python.html
So I'm not sure why it's breaking.
Very like that the script imports itself, i.e. your script is named cubehelix.py. So, if you have file named cubehelix.py in your current working directory, rename it to my_cubehelix.py and try again.