I know that such error might appear if I named the file same as importing lib, but there are must be is something different, because the error persist even if try run that code below through the console:
# -*- coding: cp1251
import _mssql
connSQLserver = _mssql.connect(
server="localhost",
port=1433,
user="admin",
password="****",
database="master")
This is output:
AttributeError Traceback (most recent call last)
<ipython-input-18-8ecbd448023e> in <module>()
2 import _mssql
3
----> 4 connSQLserver = _mssql.connect(
5 server="localhost",
6 port=1433,
AttributeError: 'module' object has no attribute 'connect'
I'm trying to connect to locally installed sql server 2014, using pymssql-2.1.1-cp27-none-win_amd64.
I tried even import pymssql , nothing changed. According to official documentation, both library might be used. I use python 2.7.1 installed as a part of Anaconda, windows 7. Connection string might be wrong.
What may cause the error?
upd:
# -*- coding: cp1251
import _mssql
print _mssql
returns
<module '_mssql' (built-in)>
print _mssql.__file__ returns AttributeError: 'module' object has no attribute '__file__'
The issue is simply that .connect(...) must be capitalized like so .Connect(...).
When you install a python module as root, sometimes, the access rights to the modules files are limited for others than root.
Juste give recurse right as root to everyone:
by example for python 2.7 module, installed through pip2.7
chmod -R a+rx /usr/local/lib/python2.7
You can check this is a permission problem by launching program through strace...
I'm not sure what gave you the idea to import _pymssql; the leading underscore should have given you the clue that this is a private utility module. Just import pymssql.
Try _mssql.MSSQLConnection
from
Related
I am getting module not found error when I am importing another python file.
Here is the code of the file which I am importing
import mysql.connector
mysql = mysql.connector.connect(host="localhost",user = "root", passwd = "#####" , database = "trading")
aa = mysql.cursor()
insert_into_table='insert into ticks(last_price,date) values(%(last_price)s,%(date)s)'
def insert_ticks(ticks):
c=mysql.cursor()
for tick in ticks:
c.execute(insert_into_table,{'last_price':tick['last_price'],
'date':tick['timestamp']})
try:
db.commit()
except Exception:
db.rollback()
I named this file as py_mysql. Now when I am importing this file to another code. It is giving an error that module not found.
I ran sys and sys.path too.
['C:\\Users\\ADMIN',
'c:\\users\\admin\\appdata\\local\\programs\\python\\python37\\python37.zip',
'c:\\users\\admin\\appdata\\local\\programs\\python\\python37\\DLLs',
'c:\\users\\admin\\appdata\\local\\programs\\python\\python37\\lib',
'c:\\users\\admin\\appdata\\local\\programs\\python\\python37',
'',
'c:\\users\\admin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages',
'c:\\users\\admin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\kiteconnect-3.8.0-py3.7.egg',
'c:\\users\\admin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\win32',
'c:\\users\\admin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\win32\\lib',
'c:\\users\\admin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\Pythonwin',
'c:\\users\\admin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\ADMIN\\.ipython']
I have placed my py_mysql file in C:\\Users\\ADMIN location.
Here is the error that I am getting
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-6-0da7c671bbff> in <module>
1 # import sys
2 # sys.path
----> 3 from py_mysql import *
4 from kiteconnect import KiteConnect
5 from kiteconnect import KiteTicker
ModuleNotFoundError: No module named 'py_mysql'
Kindly suggest what can be done to resolve this ?
Make sure your file is named py_mysql.py.
Your python may be called with py, py3, python or python3. Here I will be using python3.
Make sure your py_mysql.py is located in one of the folders listed in python3 -m site (input command in terminal).
You should no longer get the error ModuleNotFoundError when running python3 -c 'import py_mysql' from the terminal.
If you are getting the error when running it from your IDE your project interpreter may be using a different Python interpreter. Make sure the path match which python3 on Linux and where python3 on Windows.
I'm trying to run a python script on Ubuntu 16.04; the script runs fine on Ubuntu 14.04, but I keep getting kind of a vague object has no attribute error.
It seems this exception class is still active so not sure what the problem is. I've installed python-urllib3 and python3-urllib3 (even though python on the 16.04 system is a symlink to python-2.7) with no luck.
This is my error and line 507 from the code. Any way to get more info about the error?
Traceback (most recent call last):
File "./jsontest.py", line 507, in <module>
except urllib.error.URLError as e:
AttributeError: 'module' object has no attribute 'error'
#!/usr/bin/python
import urllib
import re
import json
import sys
import getopt
...
# line 507
except urllib.error.URLError as e:
print "fail: ", e.reason
...
update: As noted by #a_guest. Had to make this change. Why it runs fine on 14.04 I don't know. Maybe this was the difference bewteen 2.7.6 and 2.7.12:
except urllib2.error.URLError as e:
You linked the documentation for Python 3 however you seem to be using Python 2. urllib on Python 2 doesn't have that error module, just as the error states.
urllib2 on the other hand has this class, so you can use urllib2.URLError instead.
That error means that the urllib module contains nothing called error.
My urllib doesn't have an error submodule.
Note that Ubuntu 16.04 /usr/bin/python is a symlink to python2. If you want to use python 3.x, you should change the first line of your script from
#!/usr/bin/python
to
#!/usr/bin/python3
(Note: the "#!" at the beginning of this line is called "shebang".)
Alternatively you can create a virtual environment so that when you activate it, it will run python 3.
I have trouble setting up the Python-Path for it, I don't really know what to do here. I use elementary OS, which should be similar enough to Ubuntu.
My code so far is this:
#!/usr/bin/python3
import gi
gi.require_version('GTK', '3.0')
When I run the script, the system reports that there is no module named "gi".
However, when I try
sudo apt-get python3-gi
I get told that it is installed already.
I tried
print(sys.path)
and
locate python3-gi
which gave me back some directories. But there were too many, so I don't know what, if this is at all the right way, to to copy where.
Addendum:
Okay, so doing type export PATH="$PATH:/usr/local/bin/python-gi3" yields export is a shell builtin
bash: type: PATH=/home/ge0rg/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/bin/python3-gi: not found
uname -r yields 3.19.0-51-generic.
In the meantime, I also tried reinstalling python3-gi, to no avail. Also, since python3-gi seems to b located in /usr/lib/python3/dist-pacakges, I tried going into python and doing sys.path.append("/usr/lib/python3/dist-pacakges") where the above mentioned folder was not included before. However, after doing this import gi gives the same error as before: >>> import gi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'gi'
EDIT: Also, locate python3-gi yields:
`/usr/share/doc/python3-gi
/usr/share/doc/python3-gi-cairo
/usr/share/doc/python3-gi/changelog.Debian.gz
/usr/share/doc/python3-gi/copyright
/usr/share/doc/python3-gi-cairo/changelog.Debian.gz
/usr/share/doc/python3-gi-cairo/copyright
/usr/share/lintian/overrides/python3-gi
/var/lib/dpkg/info/python3-gi-cairo.list
/var/lib/dpkg/info/python3-gi-cairo.md5sums
/var/lib/dpkg/info/python3-gi-cairo.postinst
/var/lib/dpkg/info/python3-gi-cairo.postrm
/var/lib/dpkg/info/python3-gi-cairo.shlibs
/var/lib/dpkg/info/python3-gi.list
/var/lib/dpkg/info/python3-gi.md5sums
/var/lib/dpkg/info/python3-gi.postinst
/var/lib/dpkg/info/python3-gi.postrm
/var/lib/dpkg/info/python3-gi.prerm
/var/lib/dpkg/info/python3-gi.shlibs`
however, /usr/lib/dist-packages/gi also exists, but locate gi just gives back a whole array of mostly unrelated results.
I am guessing its installed in /usr/local/bin/python-gi3 . If you see it there, open your shell and type type export PATH="$PATH:/usr/local/bin/python-gi3" to set the path
After installing the facebook-sdk module here, and looking at other solutions here and elsewhere, I keep getting this error:
Traceback (most recent call last):
File "facebook.py", line 1, in <module>
import facebook
File "/home/facebook/facebook.py", line 3, in <module>
graph = facebook.GraphAPI(access_token='ACCESS TOKEN HERE')
AttributeError: 'module' object has no attribute 'GraphAPI'
For this very simple python code to authenticate...
import facebook
graph = facebook.GraphAPI(access_token='ACCESS TOKEN HERE')
print 'Workinnnn'
It says my module is installed and up to date, and I have installed both within (as suggested) a virtualenv and outside, and still get the error. I also definitely HAVE the module in usr/local/lib/python etc. dist packages... and it contains the class GraphAPI
Has anyone got a suggestion either:
1) What might be going wrong?
2) What to try to fix it? UNinstall something?
3) If there is another way other than pip to install the module... I don't normally use pip (but definitely have it installed and installed facebook-sdk from it) so if there's another way then I'd like to try...
Cheers :/
Solution = don't name your script the same as your module.
I'm an idiot, sigh...
I am trying to follow the example on page 5 of the book: Mining the Social Web, from O'Reilly. I am coming across the following error:
>>> import twitter
>>> twitter_api = twitter.Twitter(domain="api.twitter.com", api_version='1')
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'module' object has no attribute 'Twitter'
What might be going on?
Check the value of twitter.__file__ (after you've imported twitter). My guess is either you somehow got a broken version of twitter, or you've created a file called twitter.py in the same directory you're running from that's blocking the installed module from loading.
If twitter.__file__ looks good (points to where your installed modules should be instead of the local dir), try easy_install -U twitter to reinstall it.
Works for me. I installed twitter through easy_install, which installed the latest version (1.6.1). dir(twitter) also lists Twitter here.
You could remove the twitter package from site-packages and try reinstalling again.