python module import error - python

I am using macports to install various modules. Generally this works well, but below is an error I am getting that I am not easily resolving:
$ python
Python 2.6.6 (r266:84292, Feb 12 2011, 16:57:53)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dns
>>> import opcode
>>> from dns import resolver
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/dns/resolver.py", line 26, in <module>
import dns.message
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/dns/message.py", line 28, in <module>
import dns.opcode
ImportError: No module named opcode
Could this be a path issue?
>>> import sys
>>> sys.path
['', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']
$ cat /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/dns/init.py
[snipped comments]
# init.py for DNS class.
__version__ = '2.3.3'
import Type,Opcode,Status,Class
from Base import DnsRequest, DNSError
from Lib import DnsResult
from Base import *
from Lib import *
Error=DNSError
from lazy import *
Request = DnsRequest
Result = DnsResult
Thanks in advance.

Because you need to do:
from dns import resolver
This doesn't work:
import datetime.datetime
But this does:
from datetime import datetime
If you're importing a package that is part of another package, you need to use the "from" syntax

I uninstalled py26-dnspython and reinstalled. Problem solved. Fink on freenode made the suggestion. Thanks.

I am using Python 3.7 and I installed pubdns. This resolved my problem.
I faced extreme difficulty in using py3dns, pyDNS (won't install), dnspython and many more

Related

Why I can not import the package in Python?

I use the code from here:
https://github.com/Jefferson-Henrique/GetOldTweets-python
And every time I try to import the file folder, import got, it will raise:
Traceback (most recent call last):
File "C:\Users\USER\Desktop\python\get_old_tweet\Main.py", line 1, in <module>
import got
File "C:\Users\USER\Desktop\python\get_old_tweet\got\__init__.py", line 1, in <module>
import models
ImportError: No module named 'models'
I have check the file and been pretty sure that they do have the file folder called models
And the file folder also contains __init__.py file.
So it should work well..
I have no idea how it doesn't work. Please help me!
Which version of Python do you use?
The library https://github.com/Jefferson-Henrique/GetOldTweets-python is written with Python 2.
Python 2 and Python 3 have a bit different behavior with import: https://www.python.org/dev/peps/pep-0404/#imports
Let me share example of import regarding your case:
$ python3
Python 3.5.0 |Anaconda 2.4.0 (x86_64)| (default, Oct 20 2015, 14:39:26)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import got
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/viach/Downloads/GetOldTweets-python-master/got/__init__.py", line 1, in <module>
import models
ImportError: No module named 'models'
>>> ^C
KeyboardInterrupt
$ python2
Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import got
So, quick solution for you: use Python 2 in your app which depends on GetOldTweets-python.
Python only searches its default library paths by default (including the running script path). So you need to put them in Python default library paths or append your module path to those paths.
To append the path array:
>>> import sys
>>> sys.path.append("path/to/the_module")
>>> import the_module
If above solution doesn't worked, try:
>>> from models import got
It depends on where you are importing from. In the repository you proved a link to models is a subfolder of got. Try this:
from got import models
You can use a Python3-compatible fork of GetOldTweets-python:
https://github.com/Mottl/GetOldTweets-python3

MongoEngine import problems - Python

I'm using pymongo and mongoengine.
When I do
import mongoengine
In my /home/ubuntu/project directory
I'm getting the following error:
>>> import mongoengine
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/mongoengine/__init__.py", line 1, in <module>
import document
File "/Library/Python/2.7/site-packages/mongoengine/document.py", line 2, in <module>
import pymongo
File "/Library/Python/2.7/site-packages/pymongo/__init__.py", line 83, in <module>
from pymongo.collection import ReturnDocument
File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 17, in <module>
import collections
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.py", line 10, in <module>
from keyword import iskeyword as _iskeyword
ImportError: cannot import name iskeyword
In other directories I am able to run the import command, but not in my project directory. For example, in /home/ubuntu/ I'm getting:
>>> import mongoengine
>>>
Any clues?
****** Edit ******
There seems to be some confusion: both pymongo and mongoengine are installed using pip:
$ python
Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> import mongoengine
>>>
It looks pymongo or mongoengine is not included in your default python path.
Try installing with following command
pip install pymongo
pip install mongoengine
Source For the installation documentation and further details.
Source for mongoengine Documentation.

Getting an error when importing mechanize branch for python3

I have installed mechanize library for python3.
https://github.com/adevore/mechanize/tree/python3
But, when I import it, I get this error.
Python 3.3.3 (default, Dec 30 2013, 16:15:14)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import mechanize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Username/.virtualenvs/python3/lib/python3.3/site-packages/mechanize-0.2.6.dev_20140305-py3.3.egg/mechanize/__init__.py", line 122, in <module>
File "/Users/Username/.virtualenvs/python3/lib/python3.3/site-packages/mechanize-0.2.6.dev_20140305-py3.3.egg/mechanize/_mechanize.py", line 15, in <module>
File "/Users/Username/.virtualenvs/python3/lib/python3.3/site-packages/mechanize-0.2.6.dev_20140305-py3.3.egg/mechanize/_html.py", line 16, in <module>
ImportError: cannot import name _sgmllib_copy
But, I'm sure that mechanize is installed in the same virtualenv directory.
$ pip freeze
## FIXME: could not find svn URL in dependency_links for this package:
mechanize==0.2.6.dev-20140305
pyquery==1.2.8
Warning: cannot find svn location for mechanize==0.2.6.dev-20140305
I'm not used to operation in terminal, so I don't know how to fix this problem.
Could anyone please help me solve this problem?
Thank you in advance!
The git repository you referred to uses import wrong. The mechanize._html module imports _sgmllib_copy expecting to get mechanize._sgmllib_copy, but that way of doing imports has been deprecated in PEP 328. Rather it should be using relative imports, e.g. from . import _sgmllib_copy.
https://github.com/adevore/mechanize/tree/python3
This branch doesn't contain _sgmllib_copy.py at all. I took this file from master branch (it needs to change print smth to print (smth)). But I still don't get how import should be used. In _html.py module (it's located in mechanize folder) used
from . import _sgmllib_copy as sgmllib
Is this wrong? But from . import _beautifulsoup seems to be working.

ImportError: No module named isodate

I cant import rdflib in python. error detailed:
Python 2.7.3 (default, Jun 27 2012, 23:48:21)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rdflib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/rdflib/__init__.py", line 65, in <module>
from rdflib.term import URIRef, BNode, Literal, Variable
File "/usr/local/lib/python2.7/site-packages/rdflib/term.py", line 49, in <module>
from isodate import parse_time, parse_date, parse_datetime
ImportError: No module named isodate
I would be grateful if anybody can help.
Thanks.
If you actually install rdflib via pip, then its dependencies will come along with it (isodate included):
pip install -U rdflib
or
easy_install -U rdflib
Chances are you might have installed it directly from source, meaning you would have to take care of the deps yourself.
Information on installing pip if you dont have it already:
http://www.pip-installer.org/en/latest/installing.html
If you have easy_install, you can do: easy_install pip
It seems there is a dependency to isodate, so try installing that via your favorite PyPI-Installer (pip oder *easy_install*).

ImportError: cannot import name urandom in Eclipse/PyDev

When I attempt to run the script below in Eclipse(PyDev):
import subprocess
subprocess.call("/usr/local/bin/mitmdump")
An error is returned:
Traceback (most recent call last):
File "/usr/local/bin/mitmdump", line 19, in <module>
from libmproxy import proxy, dump, cmdline
File "/Library/Python/2.7/site-packages/libmproxy/proxy.py", line 22, in <module>
import shutil, tempfile, threading
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 34, in <module>
from random import Random as _Random
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 47, in <module>
from os import urandom as _urandom
ImportError: cannot import name urandom
If I run the same script from the bash, it works fine. What gives?
$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call("/usr/local/bin/mitmdump")
127.0.0.1 GET http://google.com/
There seems to be a related problem with Ubuntu Python ImportError cannot import urandom Since Ubuntu 12.04 upgrade but my environment is OSX.
To properly diagnose that, do the following:
Edit /usr/local/bin/mitmdump, and make:
try:
from libmproxy import proxy, dump, cmdline
except ImportError:
import sys
print 'Executable:', sys.executable
print '\n'.join(sorted(sys.path))
raise
And then check if what you're seeing is actually what you expected... (you can do those same prints in the command line to when the exception is not raised and check what's the difference and then, probably, update your PYTHONPATH inside Eclipse/PyDev).
I had the same symptoms but in python proper on the command line in Mac OS X. Found the answer here: Python: cannot import urandom module (OS X)
Not sure if it's the same problem you're having or how to invoke hash -r from Eclipse.
I would imagine your python path is net set properly. If so, python can't find the model and therefore can't import.

Categories