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

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.

Related

name 'TextPath' is not defined

I've tried running a program which imports the Pandapower library, and I get the Error:
NameError: name 'TextPath' is not defined
I installed Pandapower via pip install normally, and it works good, but it seems to have Problems with "pandapower.plotting.collections" and the class "class CustomTextPath(TextPath):"
Minimal Code:
import pandapower as pp
import pandapower.plotting as plot
TracebacK:
Traceback (most recent call last): File "-\Test.py", line 2, in <module> import pandapower.plotting as plot File "-\Python\Python310\lib\site-packages\pandapower\plotting\__init__.py", line 1, in <module> from pandapower.plotting.collections import * File "-\Python\Python310\lib\site-packages\pandapower\plotting\collections.py", line 36, in <module> class CustomTextPath(TextPath): NameError: name 'TextPath' is not defined
This is a bug in pandapower. They depend on having matplotlib installed. The code catches its absence with a try/except but still tries to use the elements it would have imported. So, do a pip install matplotlib and you should be good.

trouble importing autograd in python script

I'm trying to import autograd with the following line of code:
import autograd.numpy as np
However, I'm getting the following error when trying to run the script:
Traceback (most recent call last):
File "autograd.py", line 1, in <module>
import autograd.numpy as np
File "/home/hakon/Documents/FYS_STK4155/project2/code and plots/test/autograd.py", line 1, in <module>
import autograd.numpy as np
ModuleNotFoundError: No module named 'autograd.numpy'; 'autograd' is not a package
I've tried installing autograd through pip, pip3 and conda, but the error remains the same.
The problem is that your module (the one that you're running) has the same name that you're trying to import: autograd (.py). Try renaming your file and running it again.
aaossa's answer worked for me. If changing the module name that you are running doesn't work, please check if there is any other autograd(.py) that you created existing in your current directory. If so, you also need to change that file's name or delete it so that you can import "autograd".

Showing ImportError: cannot import name 'Message' from 'can'

I am importing CAN messages in Python and I am using python-can library. So the line in my python code here is:
from can import Message
It was working fine but after I changed the folders, I am getting the below error:
Traceback (most recent call last):
File "/home/rafi/PycharmProjects/Right Turn/CAN_simulator_Recv.py", line 12, in <module>
from can import Message
ImportError: cannot import name 'Message' from 'can' (/home/rafi/.local/lib/python3.8/site-packages/can/__init__.py)
Can anyone help me with this error? Maybe it has something to do with directory or packages? The same can library is working fine in other linux machines. I tried uninstalling python-can but it doesn't work.
Please help.
It's some kind of import bug. Try import Message like this:
from can.message import Message
Everything worked for me.

Error on calling a function from another file on python

I'm getting an error on importing a function from another file in python.
This is my file structure
In checkSpellings.py there's a function defined as spellchecker(tokenized_text) . And I'm trying to use it by importing it on main.py by following code
from checkSpellings import spellChecker
But it gives me an warning (red underline on top of both of checkSpellings(file name) and spellChecker(function name) in import code). This previously happened and I though this is purely an issue with intellisense . Because last time same thing happened (gave me the warning), but the code worked fine.
But now when I run the main.py it gives me an error saying
Traceback (most recent call last):
File "/home/pankaja/Documents/PycharmProjects/BookDigitizer/OCR_Correction/main.py", line 1, in <module>
from checkSpellings import spellChecker
ImportError: cannot import name 'spellChecker
How can I fix this ? What have I done wrong ?
IMPORTANT : Here I'm using python3.6 interpreter on an anaconda virtual environment. Might it be an issue ?
The function is spellchecker but you tried to import spellChecker.

Errors importing pyodbc

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.

Categories