Python - Can't Import ParametricModel from astropy.modeling - python

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__).

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

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

ImportError when trying to import Prov Module in Python

I'm getting this error when trying to import a module from the Prov package.
Here is the contents of my file:
#!/usr/bin/env
import sys
egg_path='/Library/Python/2.7/site-packages/prov-1.5.0-py2.7.egg/prov'
sys.path.append(egg_path)
#... rest of code
import model as prov
def main():
# Create a new provenance document
d1 = ProvDocument() # d1 is now an empty provenance document
# Declaring namespaces for various prefixes used in the example
d1.add_namespace('now', 'http://www.provbook.org/nownews/')
d1.add_namespace('nowpeople', 'http://www.provbook.org/nownews/people/')
d1.add_namespace('bk', 'http://www.provbook.org/ns/#')
# Entity: now:employment-article-v1.html
e1 = d1.entity('now:employment-article-v1.html')
# Agent: nowpeople:Bob
d1.agent('nowpeople:Bob')
And here is the output:
Traceback (most recent call last):
File "prov.py", line 6, in <module>
import model as prov
File "/Library/Python/2.7/site-packages/prov-1.5.0-py2.7.egg/prov/model.py", line 25, in <module>
from prov import Error, serializers
ImportError: cannot import name Error
Any ideas or fixes? I installed Prov using easy_install prov.
You need to rename your module file prov.py. It prevents import of the third-party library because the module name conflicts.
Make sure prov.pyc is removed.
I found the error. The name of my file that I was trying to import into was also called prov.py . It was a circular dependency issue.
Thank you guys for such quick responses!

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