I have made a module:
However I got this error when I import the count_kmers function:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'kmer_cython' has no attribute 'count_kmers'
I import my stuff like this:
from Seq_utils.fasta_parser import parse_fasta
from kmer_cython import count_kmers
from Alphabet.alphabet import iupac_dna
My function was defined like this:
It is a simple function that worked fine when I had it spread in the middle of other functions in my directory. Then I decide to organize it and now I wish to run it, but I can't.
I have set the PYTHONPATH in my terminal and other codes that I made a module are working.
Any tip to solve this problem?
Thank you all for your time.
Paulo
Related
I'm new to python and I'm currently learning objects and graphics. I imported the graphics.py file successfully but for some reason it keeps giving me an attribute error whenever I try to run GraphWin. please see below:
import graphics
win = graphics.GraphWin()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'graphics' has no attribute 'GraphWin'
I'm using Zelle's "Python Programming: An Introduction to Computer Science" and it's been helpful.
please advise.
Is the graphics.py in the same as the file you’re getting the error in?
Also, try
from graphics import *
Instead of
import graphics
i checked out this article and it proved helpful. i just had to create an extension where i added the specific sub-module to the module folder. in this case it's:
from graphics._init_ import *
win = GraphWin()
this solves the attribute error. however, it only works with the from import * formula and not the import formula
This is my first time asking a question so please excuse me.
I am following a tutorial and using the code in this folder: https://github.com/ehmatthes/pcc_2e/tree/master/chapter_12/adding_ship_image
I literally replicated this folder in vs code with all the code and the names being used correctly.
For some reason I am getting this error:
Traceback (most recent call last):
File "/Users/sammyawad/Documents/projects/alieninvasion/alien_invasion.py", line 6, in <module>
from ship import Ship
ImportError: cannot import name 'Ship' from 'ship' (/Users/sammyawad/Documents/projects/alieninvasion/ship.py)
Also, I think I have an issue with linting because it is highlighting the pygame init functions for the class even though it works.
Change it to:
from .ship import Ship
import module as instance of class like this...
from ship import Ship as s1
I have two python scripts, one has all functions I have defined (functions.py) and the other only runs those functions (running_functions.py).
I imported the functions into running_functions script using from functions import*
My problem is when I ran running_functions into python console using execfile('running_functions.py') at first worked like a charm, but if I don't close the python session and do some modifications into one function in functions.py (for example changing the number of parameters that getLabels() takes (from 4 to 5)) saved then and then I ran again running_functions.py with the same comand or when I called getLabels() I get the error:
With execfile()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "running_functions.py", line 82, in <module>
predict_labels = getLabels(pred_labels, ids_tr ,labels_tr,filenames_tr, filenames_ts)
TypeError: getLabels() takes exactly 4 arguments (5 given)
Calling the function
>>> predict_labels = getLabels(pred_labels, ids_tr ,labels_tr,filenames_tr, filenames_ts)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: getLabels() takes exactly 4 arguments (5 given)
To get it work again I have to close python session and then run again execfile() or rename functions.py or do little pythons scripts with modified function.
This is very annoying because all the code takes around 10 or 15 minutes and I don't like have a lot of little scripts. So, how can I avoid this error?
I wouldn't like to close every time the session and wouldn't like to use in each function pickle module. Is it wrong the way I imported the functions? And why python returns this error? Sorry for this silly questions
I would recommend skimming over how python imports work. In general it's considered bad practice to use glob imports like from module import *. It's not transparent and makes it difficult to take advantage of reload.
I would recommend rewriting your code to do the following :
import functions
functions.getLabels(...)
and then after you change getLabels or something, you can from the shell run the following :
reload(functions)
and that will re-import your changes without having to restart the python kernel.
I've found that I need to install 'glue' first.
But after that, this error is still there.
Traceback (most recent call last):
File "C:\Users\Saisa\Desktop\Code\python\myWebSpiderForPixiv_top100.py", line 1, in <module>
from gwpy.timeseries import TimeSeries
File "C:\Users\Saisa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gwpy\timeseries\__init__.py", line 27, in <module>
from .core import *
File "C:\Users\Saisa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gwpy\timeseries\core.py", line 45, in <module>
from ..data import (Array2D, Series)
File "C:\Users\Saisa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gwpy\data\__init__.py", line 29, in <module>
from glue.lal import (Cache, CacheEntry)
ImportError: No module named 'glue.lal'
Here is another idea of what you could try:
If I understand right, your directory hieracy looks something like this:
\root
\glue
\lal
\other
\another
and in lal there are the functions and classes and whatever you want. Is lal a directory or a file? If it is a directory, I don't understand why It is not working, I just wrote a analogue code and for me it worked perfectly. Maybe ther error is somewhere entierely else, sh*t happens in python, too. But if it is a file like lal.py you don't have to tell from glue.lal import (whatsoever)! This whatsoever are functions then in one file. Just type from glue import lal, it will import everything in there.
Your Problem is that Python does not know where to find the module. You must add the path to the Path Browser where the module code is saved in and then you can use the
import themoduleyouwant
Or
from themoduleyouwant import *
command.
You can find the Path Browser under "File >> Option >> Path Browser"
Usually, it will show you the standard libraries and your current working directory (cwd). So if you are looking for a quick solution, just take the directory of 'glue' and copy+paste it into your cwd.
Result: The Error will disappear.
I have a segmenting.py module in a package called processing.
I am trying to call a function in the module in my main. It is extremely simple.
In main.py
from processing import segmenting
segmenting.test()
In segmenting.py
def test():
print 'succeed'
However, I end up with errors as follows:
>>> from processing import segmenting
>>>
>>> segmenting.test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'test'
>>>
Where went wrong?
The most likely cause is that you didn't restart your interactive interpreter after editing (and saving!) segmenting.py. Modules are imported only once and cached. If you edit the source code and then run the import statement again, the module is simply retrieved from the cache and doesn't pick up your changes. See also the reload() built-in.