Python: can not import util - python

While reading the code for django/forms/widgets.py, I saw:
from util import flatatt
To dig deeper, I tried to import the util module in a Python shell in my terminal, but I got an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named util
This left me confused. What is wrong?

Django is (ab?)using Python's import system in order to import something from django.forms.util. Import that module instead.

In python, you get this ImportError:
Traceback (most recent call last):
File "b.py", line 3, in <module>
from util import flatatt
ImportError: No module named util
Some programmer out there created a library for your python script to use. Your program was unable to find that library. So the interpreter tells you that it can't be found. You have a few options. Either you have to define the library and functionality yourself, or you'll have to review the instructions and source code of the program you are trying to run, and try to figure out what is broken to prevent the library from being included. Or the final option is to remove any usage of that library in the current script.
You can define the module yourself like this:
Put this in util.py in the same directory as your widgets.py file.
def flatatt(prompt):
print("ok")
If you define that, you will get a different error, the script will import your library, and find that method, but then will do the wrong thing, because the programmer who designed that code probably had some other functionality in mind.
You need to figure out what you did wrong, or what the original programmer did wrong, or how your specific system is different to cause this to work.
You can learn more about what I've described above here, by rolling your own python modules: Python: How to import other Python files
Often times these sorts of bugs are barriers to entry, the original developers don't want programmers who don't know the difference between an integer and a module using the software. So little problems like this are added, to help encourage you that you need to develop a better understanding of the source code under the hood. It's like a mechanic helping everyone become better mechanics by swapping the wires on the distributor cap. It isn't going to work until you swap them back rightly.

from django.forms.utils import flatatt
It worked with django 1.11 version, and may work with 1.8 through 1.10

Related

Flask-Dance: No module named 'flask_dance.consumer.backend'

That's my very first question here, please don't eat me! I am totally newbie, I try to write a flask app using flask-dance for OAuth reason. Seems I do something stupid, because when I try to import SQLAlchemyBackend class from flask_dance.consumer.backend.sqla Python returns
ModuleNotFoundError: No module named 'flask_dance.consumer.backend'
Can you tell me please what exactly I do wrong?
I use flask-dance[sqla] version for SQLAlchemy db. First I installed basic version without SQLAlchemy support, then I uninstalled it and installed current one (all inside the venv). Is it possible that that affected the F-D functionality and its own modules and submodules scheme?
I have no idea how to fix that problem and where to go. Google doesn't help neither.
What I see in the trackback is:
Traceback (most recent call last): File "c:\users\igor\envs\2do2\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name) File "C:\Users\igor\projects\gp\2do2\app\app.py", line 8, in <module>
from flask_dance.consumer.backend.sqla import SQLAlchemyBackend ModuleNotFoundError: No module named 'flask_dance.consumer.backend'
In Flask-Dance 1.4.0, "backends" were renamed to "storages", since the word "backend" means something different in the context of web development. This change was backwards-compatible in 1.4.0, but the backwards-compatibility features were dropped in Flask-Dance 2.0.0.
To make this work in Flask 2.0 and above, just replace the word "backend" with the word "storage" everywhere you see it. For example, this line of code:
from flask_dance.consumer.backend.sqla import SQLAlchemyBackend
becomes this instead:
from flask_dance.consumer.storage.sqla import SQLAlchemyStorage
If you see any references to "backends" in the current documentation, please let me know! They should all be "storages" now.

Python for .Net Error: ImportError: No module named

We are using Python for .Net to call .NET API built using C# from Python script.
We are getting ImportError: No module named - error when an import is done as follows.
Python script:
import sys
sys.path.append(r"C:\myfolderA\myfolderB")
print sys.path
import clr
clr.FindAssembly(r"AA.BB.CC")
clr.AddReference(r"AA.BB.CC")
from AA.BB.CC.Api.DDInterface import DDClient
On the above line I am getting following error
Traceback (most recent call last):
File "C:\myfolderA\myfolderB\testAPI.py", line 7, in <module>
from AA.BB.CC.Api.DDInterface import DDClient
ImportError: No module named AA.BB.CC.Api.DDInterface
There is no other information available to exactly identify the issue.
Dlls from same project built 15 days back works fine.
This project may have gone through few changes in between.
How to exactly identify this issue?
Could this be a dependency issue?
I tried JustDecompile to compare old dlls and new ones, but couldn't find anything unusual.
Your help is deeply appreciated.
Thanks,
With help from python forum I was able to use Microsoft's Fuslogvw.exe (Assembly Binding Log Viewer) and figure out the dependent dll causing the error.
Thanks
Here is a link to my original answer:
https://mail.python.org/pipermail/pythondotnet/2014-December/001626.html
I recommend using Assembly Binding Log Viewer (fuslogvw.exe) for
corresponding .NET framework to see the log files of loading assemblies.
Set Log Location as Custom and in Settings point to your assemblies folder.
The log should show the sequence of DLLs being loaded and any errors.
This worked for me, see here:
Could not load file or assembly or one of its dependencies
Can't locate fuslogvw.exe on my machine

Override installed module in import

I am currently working on a package that's nearly ready for distribution. As such, I'm trying out installing it (via setup.py) on my system. However, I also need access to the current (source code) package, e.g. for tests.
Is it possible to explicitly force importing the local package instead of the installed version? What I already tried is adding the path at the beginning of sys.paths, with no luck.
I guess this question goes into how exactly Python looks for the modules to import, and how to change the order.
EDIT : It was a silly mistake, adding at the beginning of sys.paths works. I had some other import statement elsewhere that was executed first.
This is bad bad idea to do something like force importing the local package.
Python3 always import the built-in modules first, and obviously you're using Python3 cause Python2 is local-first.
I don't know how to solve this problem, if I were you I'd rename that file.
edit
You don't use append cause append is to the end. Use insert.
Tested on my cpu. I created a requests.py in another folder.
>>> import sys
>>> sys.path.insert(0 ,"path-to-another-folder/")
>>> import requests
>>> requests.get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'get'
>>>
If I don't do sys.path.insert(0 ,"path-to-another-folder/"), requests is imported normally.

Trouble importing Python modules on Ninja IDE

I have been trying to import modules into Ninja IDE for python. These are modules that I have working on the terminal (numpy, scipy, scitools, matplotlib, and mpl_toolkits), but will not run correctly in Ninja.
First I was only getting the message No module named ____. I checked sys.path and found that the path was within the application
/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7 was a typical path. I tried changing the path,but it doesn't seem to do anything to sys.path even after restarting the ide.
But I wanted the path to refer to where the modules are stored (which is /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages). I was able to get numpy and scipy to work as well as parts of mpl_toolkits by adding the contents of my path to the folders that sys.path gave. However, I still can't get fully functioning modules within the ninja ide interpreter. I'll give some examples below of what happens when I import certain modules.
import matplotlib.pyplot
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7/matplotlib/__init__.py", line 106, in <module>
ImportError: No module named sysconfig
import mpl_toolkits
from mpl_toolkits.mplot3d import axes3d
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7/mpl_toolkits/mplot3d/__init__.py", line 1, in <module>
File "/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7/mpl_toolkits/mplot3d/axes3d.py", line 14, in <module>
File "/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7/matplotlib/__init__.py", line 106, in <module>
ImportError: No module named sysconfig
Thanks for the help. I apologize, I am very new to programming, but I did put in about a day and a half of research before posting here.
That's strange as the sysconfig module is a part of Python 2.7 standard library.
Are you sure that Ninja is using the right Python version? Try running:
import sys
print sys.version_info
from Ninja, to see which Python version it is actually using.
I know this question is a few months old, but I wanted to post my solution in case others find it useful. I had a very similar problem, and had a lot of trouble finding a quick workable solution anywhere.
My somewhat roundabout solution was to simply create a virtualenv folder with the version of numpy I wanted, and then pointed the "virtualenv" property for NinjaIDE project to that folder. I restarted NinjaIDE and boom, instantly worked.
To set the virtualenv property for your project via the GUI, go to the Project menu:
Project > Open Project Properties > Project Execution,
and you should see a variable called "Virtualenv Folder". Point that to the folder for your virtualenv, and it should work. (May need to restart NinjaIDE.) This worked for me, NinjaIDE version 2.2 under Ubuntu 12.04.
One quick note: I actually didn't use virtualenv exactly -- I had to use a "conda env," since I am using the Anaconda distribution, and apparently it is not well-tested with virtualenv yet. (I actually got a warning when I went to easy_install virtualenv. Hadn't seen that before.)
Either way, this stackoverflow question has some nice pointers to virtualenv tutorials: Comprehensive beginner's virtualenv tutorial?
Good luck!
I was having a similar problem trying to import a module from /home/paul/lib/python using the console of the Ninja-IDE. I found out that /home/paul/lib/python didn't appear in syspath when checking in the console of the Ninja-IDE. But it did in the terminal!
By starting the Ninja-IDE from the terminal, /home/paul/lib/python was in syspath when checking in the console of the Ninja-IDE. I was now able to import the module I needed.
I hope this might be of some help. If not to ebris1 than maybe to others.

LPTHW ex50:Can't find module utils

I'm at exercise 50 of lpthw this is the link.
And after having downloaded the lpthw.web framework in a very messy way.
I continued the exercise by writing a .py file.
import web
urls = ('/', 'index')
app = web.application(urls, globals())
class index:
def GET(self):
greeting = "Hello World"
return greeting
if __name__ == "__main__":
app.run()
but i got this error:
Traceback (most recent call last):
File "bin\app.py", line 1, in <module>
import web
File "c:\Python31\lib\site-packages\web\__init__.py", line 14, in <module>
import utils, db, net, wsgi, http, webapi, httpserver, debugerror
ImportError: No module named utils
What is the problem caused by,
I suspect it is the very messy way with wich I installed lpthw.web (I got a lot of errors, but tried multiple ways multiple times.)
Thank you in advance!
If anybody needs more info comment so I can edit.
I think you forgot to read the setup instructions. This learning guide is not python3 compatible:
Exercise 0: The Setup
You should follow these instructions as exactly as possible. For
example, Mac OSX computers already have Python 2, so do not install
Python 3 (or any Python).
Make sure you install Python 2 not Python 3.
Warnings For Beginners
A programmer may try to get you to install Python 3 and learn that. You should tell them, "When all of the python code on your computer is Python 3, then I'll try to learn it." That should keep them busy for about 10 years.
Remove python3 from your system and install python2.7 to allow this tutorial to work when you install suggested packages.
Otherwise, find another tutorial that is using examples from libararies that are python3 compatable. Not everything is updated as of right now. Many people stick to python2.7 for this reason.
As the tutorial state, you can try installing using easy_install or pip from the command line it would be easy_install lpthw.web or pip install lpthw.web
Also note that you need to properly reference application
this app = application(urls, globals()) should be
app = web.application(urls, globals())
note the ^^^ part.
Also if you are interested in python micro-web-frameworks I recommend bottle.py, it doesn't need to be installed its just a single python file called bottle.py
Update
I managed to recreate the problem using python3, so jdi was right, the problem here is that python3 isn't backwards compatible with python2, some python2 complaint applications won't properly run under python3 and vice-versa.
In this case python3 doesn't seem to support relative imports within package, did find this http://www.python.org/dev/peps/pep-0328/ which was kind of interesting.
exert from the pep:
For the second problem, it is proposed that all import statements be absolute by default (searching sys.path only) with special syntax (leading dots) for accessing package-relative imports.
so lpthw.web tries to import relative packages but it simply can't.
which is why we get ImportError: No module named utils since utils is relative module within the package.
even if it could loaded it, it will stumble with other non-backwards compatibility issues, just trying to import utils within the package we get
>>> import utils
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "utils.py", line 75
except KeyError, k:
^
SyntaxError: invalid syntax
which is related to this pep http://www.python.org/dev/peps/pep-3110/ which states different syntax to the except statement.
As you can see it will be a major hurdle trying to migrate this package to python3 so much so thats its better to leave it to the developers, this goes for the many many packages out there, why is python3 so different from python2, well mainly because they wanted to patch the language as much as possible ...
For the time being stick to python2, eventually most packages will be migrated to python3.

Categories