module "twitter" has no attribute "Twitter" - python

I have the following file, twitter.py, which defines a class called Twitter:
class Twitter:
data = {}
def __init__(self):
pass
def tweet(self):
print("I'm tweeting")
And I have another file, main.py, in the same directory as twitter.py which imports twitter and attempts to instantiate the class:
import twitter
twitterObj = twitter.Twitter()
Unfortunately, Python throws the error message: AttributeError: module 'twitter' has no attribute 'Twitter'
What am I doing wrong?

Can't reproduce this error on my machine. I'm assuming you have a module with the name "twitter" installed to your python path that is overriding the project file. Try renaming twitter.py.

Related

ModuleNotFoundError : No Module named 'tests'

I have a test automation project where so far everything was working great.
I was able to run all the test by setting the Python path by typing the below command:
set PYTHONPATH="projectpath"
then
python .\"testscriptpath"
Now this week I started seeing this error:
ModuleNotFoundError : No Module named 'tests'
I tried the solution of adding a blank __init__.py file, but it didn't help.
I am looking for a solution to generate XML report files.
Below is the code:
import unittest
import allure
import xmlrunner
from selenium.webdriver.common.by import By
from tests.common import WICSUtils
from tests.common.GenericUtils import wics_select_by_visible_text, wics_utils_click, wics_select_by_index, \
wics_utils_get_text
from tests.icc.ICC_Common_Methods import search_by_offender_id_icc, make_initial_decision, \
go_to_inmate_classification_report, go_to_job_submitter_screen_and_submit_report, \
refresh_job_queue_until_job_finished
class ICCInmateInitialClassification(unittest.TestCase):
#classmethod
def setUpClass(cls):
# Get new driver instance
global myDriver
global emailAddress
global userFolder
myDriver = GenericUtils.get_new_driver_instance()
#allure.step("Logging into WICS")
def test_01_logging_into_WICS(self):
global emailfolderforreports
emailfolderforreports = "Reports"
WICSUtils.loginToWICS(myDriver, WICSUtils.Environment.UAT1, test)
expectedTitle = "ODSP590 - My Landing Page"
actualTitle = WICSUtils.get_page_main_title(myDriver)
GenericUtils.wics_assertion_is_substring(expectedTitle, actualTitle, myDriver)
#classmethod
def tearDownClass(cls):
WICSUtils.logOutWICS(myDriver)
myDriver.quit()
if __name__ == '__main__':
# main method to run xmlrunner to produce xml report in test-reports folder
with open('../../../test-results/ICC_R001_Inmate_Initial_Classification.xml', 'wb') as output:
unittest.main(
testRunner=xmlrunner.XMLTestRunner(output=output),
failfast=False, buffer=False, catchbreak=False)
Below is the error stack trace:
PS C:\Users\VellaSR\PycharmProjects\wics-selenium-scripts> python .\tests\icc\High\ICC_R001_Inmate_Initial_Classification.py
Traceback (most recent call last):
File ".\tests\icc\High\ICC_R001_Inmate_Initial_Classification.py", line 8, in <module>
from tests.common import WICSUtils
ModuleNotFoundError: No module named 'tests'
In order to make Python resolves all your relative imports, you must execute your script from the root working directory.
In your case, for example, the root is wics-selenium-scripts. You need to go there with your terminal, and then execute python path/to/your/script.py, e.g. python tests\icc\High\scriptName.py

How to import a user defined class from a Package to a Jupyter Notebook

I'm trying to create a project with the following structure:
my_file.ipynb
my_package_directory > __init__.py test.py
Within test.py lets say I have a very simple class:
class Test:
def __init__(self, name):
self.name = name
from with in a code cell I try to insatiate the class and print out the defined variable:
from my_package_directory.test import Test
test = Test('bob')
print(test.name)
If I try to run the cell I get an error:
ImportError: cannot import name 'Test' from 'my_package_directory.test'
Is there a certain way to do this in a Jupyter notebook?
Thank you.
Probably the root of your files is not in the python module search path.
You can check your module search path with:
import sys
print(sys.path)
If you append the location of your notebook and your package to that path, it should be importable:
sys.path.append('/path/to/where/jupyter/notebook/resides')
from my_package_directory.test import Test # should work
Note that you have to restart the kernel whenever you change the implementation of your module - This can be circumvented with some jupyter notebook magic.

Can anyone help me howto import a module class?

I have a module example.py with the follow class:
class add:
def __init__(self, x, y):
self.x= x
self.y= y
def adding_two_nbr(self):
return self.x*self.y
when I import the module as the following i got error:
import example
obj = example.add(1,2)
obj.adding_two_nbr()
error:
AttributeError: 'add' object has no attribute 'adding_two_nbr'
The code works for me smoothly. Change the second block of code as
import example
obj = example.add(1,2)
print(obj.adding_two_nbr())
Save this file as import_file.py in the same directory as example.py and run import_file.py.
I think add class is already defined in python module so you need to change to another name
AttributeError: 'add' object has no attribute 'adding_two_nbr'
adding_two_nbr not defined in python add module.

AttributeError: module 'unittest' has no attribute 'Testcase'

I am new with python, i am trying to fllow the tutorial on youtube https://www.youtube.com/watch?v=6tNS--WetLI&t=168s
I have the error message
code:
import unittest
import calc
class TestCalc(unittest.Testcase):
def test_add(self):
result=calc.add(10,5)
self.assertEqual(result,15)
suite = unittest.TestLoader().loadTestsFromTestCase(TestCalc)
unittest.TextTestRunner(verbosity=2).run(suite)
I already seen all the answer on stackoverflow but the error resist.
Please help
You did a typo when writing the class name. It is with capitalized case TestCase, like you can see in the documentation.

importing error in python while importing two classes

I am working with a python script, and i face importing problem when i try to import a class from another python script. Here is how my python project folder looks:
Mysql_Main/
checks.py
Analyzer/
config.py
ip.py
op.py
__init__.py
Now i want to import two classes named: Config() and Sqlite() from config.py into the checks.py script.How do i do it?
This is what i tried, but its resulting in an error!
inside checks.py:
from Analyzer import config
config = config.Config()
sqlite = config.Sqlite()
The problem is that Config class is imported properly, but Sqlite class is not getting imported.It is showing error - Config instance has no attribute 'Sqlite'
When you do:
config = config.Config()
You write over the variable config and it no longer points to the module config. It stores the new Config instance.
Try:
from Analyzer import config
config_instance = config.Config()
sqlite_instance = config.Sqlite()

Categories

Resources