ImportError: cannot import name is_python_keyword - python

I am trying to execute a python script , but I get an error on line
from jinja2.utils import Markup, concat, escape, is_python_keyword, next
ImportError: cannot import name is_python_keyword
I checked there is no file named is_python.py

Looking at the source code for 2.3.1 they have a line:
from keyword import iskeyword as is_python_keyword
They are using the builtin keyword module.
The current version is 2.7.3 so it seems they have changed the code and it is no longer available.
You could use the above import from the builtin module instead.

Related

Attribute error: module 'collections' has no attribute 'MutableSequence' PYTHON/SMARTSHEET SDK

I installed the Smartsheet Python sdk, imported the smartsheet module but am getting an error when I want to run the script. The error is localized to the smartsheet module and says that collections module is missing Mutable Sequence. I have already tried adding:
from collections.abc import MutableSequence
and had no change.
import smartsheet
import logging
import os
_dir = os.path.dirname(os.path.abspath(__file__))
This is what pops up in the terminal.
File "C:\Users\jhorvath\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\smartsheet\smartsheet.py", line 34, in <module>
from .models import Error, ErrorResult
File "C:\Users\jhorvath\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\smartsheet\models\__init__.py", line 21, in <module>
from .access_token import AccessToken
File "C:\Users\jhorvath\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\smartsheet\models\access_token.py", line 20, in <module>
from ..types import *
File "C:\Users\jhorvath\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\smartsheet\types.py", line 29, in <module>
class TypedList(collections.MutableSequence):
AttributeError: module 'collections' has no attribute 'MutableSequence'
A "quick and dirty" solution (I have this one - Python 3.10):
Path you-python-installation\python\Lib\site-packages\smartsheet
File
types.py
Line 29
class TypedList(collections.MutableSequence):
Replace with
class TypedList(collections.abc.MutableSequence):
I think Сolleagues from smartsheet - will fix the compatibility problem (at least - I believe it)
The main reason is
Deprecated since version 3.3, will be removed in version 3.10
Another solution I used is to modify the affected .py file in downloaded library from Smartsheet and change
import collections
to
import collections.abc as collections
If you are not able to edit the package as recommended in the first solution, use Python 3.9 that is the last version to support collections Mutable Sequence.

ImportError: No module named json; python 2.6.6

I have installed Jmespath and it is internally using import json statement in lexer.py. I cannot change their source to import simplejson as json. HEnce, i wanted to know is there any work around to make this work?
File "/usr/lib/python2.6/site-packages/jmespath/lexer.py", line 3, in <module>
from json import loads

importing library in python

Am trying to run a python script that i downloaded from the internet
import os, subprocess, sys, socket, time, struct, random, xml.sax, getopt
import shutil
import Output
import numpy as np
...
I get the error
Traceback (most recent call last):
File "downtown.py", line 20, in <module>
import Output
ImportError: No module named Output
Am totally new to python , and I want to know whether the missing import is python's or a user library
As a first hint, observe the missing module Output starts with a capital O, which fails to follow convention of using lowercase-only for module names. Therefore Output is most certainly a user library. Alternatively, Output might be a class that would correctly need to be imported via from somelib import Output.
It is not a standard module - it looks like a user module that you should also download from wherever you got the original script.

How can you see what import fails in an imported module?

I need to make a portable app to put on a device and I want to put only the modules needed so there will not be all the standard modules so I need to see what my app needs, but if that module misses some imports I cannot see that, because it gives an error without being explicit what fails in that module:
Traceback (most recent call last):
File "./packaging.py", line 30, in <module>
import simplejson
ImportError: No module named simplejson
Is there a way to see what import exactly fails in that module?
The error means, that the import import simplejson fails because there is “[n]o module named simplejson”. The solution is to simply install said module.
It could be that the string /usr/lib/python2.7/dist-packages is not in your sys.path folder list so it's not being searched when attempts are made to import modules or packages.
you can catch the exceptions, to know what went wrong and handle them appropriately:
try:
import simplejson
except ImportError:
print "simplejson module not found"
#or do something else here, may be install that module

Why am I getting the following error in Python "ImportError: No module named py"?

I'm a Python newbie, so bear with me :)
I created a file called test.py with the contents as follows:
test.py
import sys
print sys.platform
print 2 ** 100
I then ran import test.py file in the interpreter to follow an example in my book.
When I do this, I get the output with the import error on the end.
win32
1267650600228229401496703205376
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named py
Why do I get this error and how do I fix it? Thanks!
Instead of:
import test.py
simply write:
import test
This assumes test.py is in the same directory as the file that imports it.
This strange-looking error is a result of how Python imports modules.
Python sees:
import test.py
Python thinks (simplified a bit):
import module test.
search for a test.py in the module search paths
execute test.py (where you get your output)
import 'test' as name into current namespace
import test.py
search for file test/py.py
throw ImportError (no module named 'py') found.
Because python allows dotted module names, it just thinks you have a submodule named py within the test module, and tried to find that. It has no idea you're attempting to import a file.
You don't specify the extension when importing. Just do:
import test
As others have mentioned, you don't need to put the file extension in your import statement. Recommended reading is the Modules section of the Python Tutorial.
For a little more background into the error, the interpreter thinks you're trying to import a module named py from inside the test package, since the dot indicates encapsulation. Because no such module exists (and test isn't even a package!), it raises that error.
As indicated in the more in-depth documentation on the import statement it still executes all the statements in the test module before attempting to import the py module, which is why you get the values printed out.

Categories