from typing import generator fail - python

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

Related

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

Cannot import a function from a module in 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.

How do I import symbols inside a Python package?

I have three database-related classes that I want to combine into a package, in the following structure:
adodb_pyodbc /
__init__.py # empty
PyConnection.py
PyRecordset.py
PyField.py
This package is in my Lib/site-packages folder.
In an earlier iteration of this attempt, I did not use the "Py" prefix, and I got an error complaining that module.__init__() takes only two arguments, and three were being passed into it. Someone suggested that the name "Recordset" might be conflicting with something else, so I changed it.
The classes work when the files are in the same folder as the project that is using them. In that case, I can just use:
PyRecordset.py:
from PyConnection import PyConnection
from PyField import PyField
class PyRecordset: pass
DerivedSet.py
from PyRecordset import PyRecordset
class DerivedRecordset(PyRecordset): pass
But the same files don't work when they are located inside a package. My test program begins with this line:
from adodb_pyodbc import PyConnection as Connection
And when I run it I get this error message:
C:\Python35\python.exe "C:/Customers/Nucor Crawfordsville/Scripts/64 bit/Testing/cpsa_simulator.py"
Traceback (most recent call last):
File "C:/Customers/Nucor Crawfordsville/Scripts/64 bit/Testing/cpsa_simulator.py", line 8, in <module>
from Level3_CoilsSet import Level3_CoilsSet
File "C:\Customers\Nucor Crawfordsville\Scripts\64 bit\Testing\Level3_CoilsSet.py", line 1, in <module>
from adodb_pyodbc import PyRecordset as Recordset
File "C:\Python35\lib\site-packages\adodb_pyodbc\PyRecordset.py", line 9, in <module>
from PyConnection import PyConnection
ImportError: No module named 'PyConnection'
But when editing PyRecordset.py inside PyCharm, it appears to be able to find the PyConnection.py file.
I tried using relative addressing inside PyConnection.py:
from . import PyConnection
from . import PyField
But that puts me back to the __init__() error:
C:\Python35\python.exe "C:/Customers/Nucor Crawfordsville/Scripts/64 bit/Testing/cpsa_simulator.py"
Traceback (most recent call last):
File "C:/Customers/Nucor Crawfordsville/Scripts/64 bit/Testing/cpsa_simulator.py", line 8, in <module>
from Level3_CoilsSet import Level3_CoilsSet
File "C:\Customers\Nucor Crawfordsville\Scripts\64 bit\Testing\Level3_CoilsSet.py", line 3, in <module>
class Level3_CoilsSet(Recordset):
TypeError: module.__init__() takes at most 2 arguments (3 given)
How am I supposed to do this?
Thanks very much for your help. In the meantime, I'm going to take those files out of the package and put them back in my test project. I've wasted far too much time on this question.
When you one to use PyConnection from outside of the package you have to either import it from the module where it was defined:
from adodb_pyodbc.PyConnection import PyConnection as Connection
Or, more conveniently, import it in the package init file adodb_pyodbc/__init__.py:
from .PyConnection import PyConnection
And then, from the outside, you can just do:
from adodb_pyodbc import PyConnection as Connection

Import with a comma after the as

I was looking at a repo and came across a somewhat weird line
from flask.ext.testing import TestCase as Base, Twill
What does it mean to import like this? I have not seen it before and unfortunately it's rather hard to google.
That line tells Python to import TestCase and Twill from the package flask.ext.testing, but to import TestCase under the name of Base.
From the docs:
If the module name is followed by as, then the name following as is
bound directly to the imported module.
Below is a demonstration with the search and match functions from the re module:
>>> from re import search as other, match
>>> match # The name match refers to re.match
<function match at 0x02039780>
>>> other # The name other refers to re.search
<function search at 0x02048A98>
>>> search # The name search is not defined because it was imported as other
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'search' is not defined
>>>

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