Errors importing pyodbc - python

My background is Java/C++ and I'm very new to python. I'm trying to import pyodbc to my project which works alright if I do it via command line.
import odbc
However, when I try to do the same in pydev/eclipse, i get the following error which I cannot find a solution to. I suspect this may have something to do with Eclipse setup
Traceback (most recent call last):
File "C:\Users\a\workspace\TestPyProject\src\helloworld.py", line 2, in <module>
import pyodbc
File "C:\Users\a\AppData\Local\Continuum\Anaconda3\Lib\site-packages\sqlalchemy\dialects\mssql\pyodbc.py", line 105, in <module>
from .base import MSExecutionContext, MSDialect, VARBINARY
ImportError: attempted relative import with no known parent package
I'm really stuck here and any hints will be appreciated!

An incorrect library was imported into the External Libraries list of the project. After fixing it, the import is working.

Related

Cannot get pandas to import properly

I have spent the last hour trying to fix the pandas import error, but with no success.
Python version: 3.8.3
Pandas version: 1.0.4
I use Visual Studio code. Importing other modules works (tried with numpy). Pandas is installed using pip.
The error:
Traceback (most recent call last):
File "c:/Users/38599/Desktop/course1/moduli.py", line 34, in <module>
import pandas
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pandas\__init__.py", line 180, in <module>
import pandas.testing
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pandas\testing.py", line 5, in <module>
from pandas._testing import (
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pandas\_testing.py", line 404, in <module>
RANDS_CHARS = np.array(list(string.ascii_letters + string.digits), dtype=(np.str_, 1))
AttributeError: module 'string' has no attribute 'ascii_letters'
I've tried downgrading pandas and reinstalling it, but nothing fixed the error for me.
Does anyone have any ideas?
remove the line with string.ascii_letters from the code.. as the error says
I faced same issue. I did the following and it worked.
select the py file you want to execute, for vs code menu select Run=>Start Debugging, it works perfectly. I think it is a bug in the current vs code
Do you have a file named string.py somewhere in your workspace, by chance?
If yes, that would be the problem. By Python rules, such a file is also a module named string, so whenever anything is trying to import string, your file gets loaded instead of the standard library module with the same name.

Python 'cannot import name 'TestCase'' When importing unittest

Whenever I attempt:
import unittest
I get the following error:
Traceback (most recent call last):
File "wackyTesting.py", line 1, in <module>
import unittest
File "C:\.....\Python\Python36\lib\unittest\__init__.py", line 59, in <module>
from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
ImportError: cannot import name 'TestCase'
This happens regardless of where the code is run, there is nothing in the root that could be causing a conflict. I have tried re-installing unittest but it didn't help. It was working perfectly until just recently. Any ideas?
Welp, I just uninstalled python completely and reinstalled. Nothing quite like turning it off and on again. That seems to have done it, maybe there's a more elegant solution but this will have to do for now.

Unable to import pandas module; python prints AttributeError from older python session

I have a weird issue. Whenever I import pandas, I get an AttributeError:
import pandas as pd
AttributeError: 'module' object has no attribute 'read_sql_query'
That error message is from an old python session when I was playing around with pandas. I'm now unable to load pandas as all, I just get that attribute error. Here's the full stacktrace:
>>> import pandas as pd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tsd/p19/home/p19-jonasmst/.local/lib/python2.7/site-packages/pandas/__init__.py", line 13, in <module>
__import__(dependency)
File "/cluster/software/VERSIONS/python_packages-2.7_3/cluster/software/VERSIONS/python2-2.7.9/lib/python2.7/site-packages/pytz/__init__.py", line 29, in <module>
from pkg_resources import resource_stream
File "build/bdist.linux-x86_64/egg/pkg_resources.py", line 74, in <module>
File "parser.py", line 12, in <module>
asid_occurrences = pd.read_sql_query(sql_find_asid_occurrences, db)
AttributeError: 'module' object has no attribute 'read_sql_query'
Possibly relevant info:
I'm on a cluster and have to load python2 as a module in order to import pandas.
I'm logged in to a Linux VM on the cluster through a Thinlinc client (via FireFox), from which I'm again SSHed into another Linux VM.
I've tried module purge and then module load python2 again, to no avail.
I've tried to log out from the VM and back in again, to no avail.
I was using VIM when I first got the AttributeError.
Anyone have any idea what's going on here? Thanks!
Update:
This seems to happen only when I'm in the directory where I got the error earlier. Moving to another directory allows me to import pandas just fine. There are no hidden files or anything in that directory, though, so I don't understand what's going on here.
The problem was that my script was called parser.py which probably conflicts with something in pandas. Similar issue.

error related to sqlalchemy in python

I am trying to compile a model writte in fortran called pywofost. I followed the steps of compiling the model. however, I receive the following error related to sqlalchemy.exceptions. sqlalchemy version is '1.0.12'. the error message is as follows:
command
import pywofost
error
/usr/lib/python2.7/dist-packages/pkg_resources.py:1031: UserWarning: /home/omar/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
warnings.warn(msg, UserWarning)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/omar/pywofost/pywofost.py", line 16, in <module>
from sqlalchemy.exceptions import *
ImportError: No module named exceptions
That's because there is no module in sqlalchemy named exceptions as of version 1.0.12. If you were depending on a third party module it looks like they have a bug in their code
There is sqlalchemy.exc or sqlalchemy.orm.exc if you want to import exceptions from there though. Maybe you can just modify the pywofost code quickly and continue your work?
the problem was solved by using older version of sqlalchemy

Python: multiple installations

I was successfully using python in my programs and importing necessary modules including MySQL. However, to get IDLE on my computer I downloaded a new copy of python. I'm really not sure what I did or what happened but when I try to run a program now that uses MySQL I get the following error:
Traceback (most recent call last):
File "MyFile.py", line 11, in <module>
import MySQLdb
File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so, 2): Symbol not found: _mysql_affected_rows
Referenced from: /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so
Expected in: flat namespace
in /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so
any ideas on how to fix this?
Actually I fixed the problem by reinstalling MySQLdb following the step found here. I'm not sure how well this will work for additional modules though

Categories