Cannot import a function from a module in python - python

I'm using this code.
from azlyrics import artists
print(artists("O"))
In the module named 'azlyrics' the function 'artists' is well defined. But I get this error.
Traceback (most recent call last):
File "C:\Users\user\Desktop\python\eminem\New folder\azlyrics-master\examples\get_artists.py", line 1, in <module>
from azlyrics import artists
ImportError: cannot import name 'artists' from 'azlyrics' (C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\azlyrics\__init__.py)
What could be the problem?

There must be a bug in the azlyrics documentation or packaging.
This works:
>>> from azlyrics.azlyrics import artists
>>> artists("O")
'["Oakenfold, Paul", "Oakes, Ryan", "Oak Ridge Boys,
The", "Oak, Winona", "O.A.R. (Of A Revolution)", "Oasis", "Obel, Agnes", "Oberst, ...]'
There is a mistake in azlyrics v1.3.2, relative import should be used in azlyrics/__init__.py:
instead of:
from azlyrics import *
we should have:
from .azlyrics import *
This is fixed but a release is not done.

Related

from typing import generator fail

I need help interpreting the following error message and possibly troubleshooting what steps to take next as my google searching hasn't gotten me anywhere.
>>> from typing import asyncgenerator
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
from typing import asyncgenerator
ImportError: cannot import name 'asyncgenerator' from 'typing' (C:\Program Files\Python39\lib\typing.py) ```
It should be:
from typing import AsyncGenerator
AsyncGenerator uses capital letters. Technically, asyncgenerator does not exist because the name does not match exactly.
It looks like the name is not supposed to be all lowercase. Try the command
from typing import AsyncGenerator

how to import module kicost and pass arguments

I'm trying to import the module kicost in the python script and call the main function with arguments.
So far, I've been unsuccessful to do it after many trials.
The module has been installed with pip.
import sys, os
from kicost import *
import importlib
spam_spec = importlib.util.find_spec("kicost")
print(spam_spec)
# sys.argv.append('-h')
main()
exit()
Here is the execution log:
ModuleSpec(name='kicost',
loader=<_frozen_importlib_external.SourceFileLoader object at
0x101278160>,
origin='/Users/sebo/Projects/python/pandas/env/lib/python3.8/site-packages/kicost/init.py',
submodule_search_locations=['/Users/sebo/Projects/python/pandas/env/lib/python3.8/site-packages/kicost'])
Traceback (most recent call last): File "test-import-kicost.py",
line 12, in
main() NameError: name 'main' is not defined
I guess there is something I don't understand with the import.
Could somebody help me? Thanks.
S/
Finally, I managed to do it:
import sys, os
import kicost.__main__ as kicost
sys.argv.append('-i=/Users/sebo/Projects/python/pandas/test.csv')
sys.argv.append('--currency=EUR')
sys.argv.append('--overwrite')
kicost.main()
exit()

Is it possible to import a python file from outside the directory the main file is in?

I have a file structure that looks like this:
Liquid
|
[Truncate]
|_General_parsing
[Truncate]
.....|_'__init__.py'
.....|_'Processer.py'
.....|'TOKENIZER.py'
|_'__init__.py'
|_'errors.py'
[Truncate]
I want to import errors.py from Processer.py. Is that possible? I tried to use this:
from ..errors import *; error_manager = errorMaster()
Which causes this:
Traceback (most recent call last):
File "/Users/MYNAME/projects/Liquid/General_parsing/Processer.py", line 17, in <module>
from ..errors import *; error_manager = errorMaster()
ImportError: attempted relative import with no known parent package
[Finished in 0.125s]
I've seen this post, but it's no help, even if it tries to solve the same ImportError. This isn't, either (at least not until I edited it), since I tried:
import sys
sys.path.insert(1, '../Liquid')
from errors import *; error_manager = errorMaster()
That gives
Traceback (most recent call last):
File "/Users/MYNAME/projects/Liquid/General_parsing/Processer.py", line 19, in <module>
from errors import *; error_manager = errorMaster()
ModuleNotFoundError: No module named 'errors'
[Finished in 0.162s]
EDIT: Nevermind! I solved it! I just need to add .. to sys.path! Or . if .. doesn't solve your problem. But if those don't solve your problem: use some pathlib (came in python3.4+) magic and do:
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).parent))
or, if you want to use os: (gotten from this StackOverflow answer)
import os
os.path.join(os.path.dirname(__file__), '..')
I solved it! I have to add .. to sys.path instead

Python - Can't Import ParametricModel from astropy.modeling

I am trying to import ParametricModel as follows:
from astropy.modeling import ParametricModel
But it shows following error:
Traceback (most recent call last):
File "", line 1, in
ImportError: cannot import name ParametricModel
Why can't I import ParametricModel?
That class was renamed in Astropy v0.4. See the API changes in the changelog for details (under the astropy.modeling section). Make sure any documentation you're looking at matches your astropy version (via astropy.__version__).

ImportError: cannot import name <classname>

I have this:
import sys, struct, random, subprocess, math, os, time
from m_todo import ToDo
(rest)
Which results in:
Traceback (most recent call last):
File "6.py", line 2, in <module>
from m_todo import ToDo
ImportError: cannot import name ToDo
My m_todo module:
import os
class ToDO:
'''todo list manager'''
def __init__(self):
pass
def process(self):
'''get todo file ready for edition'''
print(os.path.exists('w_todo.txt'),'\t\t\tEDIT THIS')
I read some similar questions, which suggested something about circular references, but it is not the case.
I also saw a suggestion about using relative imports, but trying that resulted in another error:
Traceback (most recent call last):
File "6.py", line 2, in <module>
from .m_todo import ToDo
SystemError: Parent module '' not loaded, cannot perform relative import
This is like the third time I use Python, so it might be a silly mistake, but it's causing me some confusion since I'm importing other modules in the same way without any issues.
So... what's going on here?
Your class is called ToDO (note the capitalisation), not ToDo.
Either fix your import:
from m_todo import ToDO
or the classname:
class ToDo:

Categories

Resources