My error is like
(base) C:\Users\lenovo\projects\gothonweb2>python bin/app.py
Traceback (most recent call last):
File "bin/app.py", line 2, in <module>
from gothonweb2 import map
ModuleNotFoundError: No module named 'gothonweb2'
The project directory is
C:\Users\lenovo\projects\gothonweb2
bin
__init__.py
app.py
gothonweb2
__init__.py
web.py
templates
layout.html
show_room.html
you_died.html
tests
__init__.py
app_tests.py
map_tests.py
tools.py
The code in bin/app.py is :
import web
from gothonweb2 import map
import urllib.request
urls=(
'/game','GameEngine',
'/','Index',
)
app=web.application(urls,globals())
#Title hack so that debug mode works with sessions
if web.config.get('_session') is None:
store=web.session.DiskStore('sessions')
session=web.session.Session(app,store,initializer={'room':None})
web.config._session=session
else:
session=web.config._session
render=web.template.render('templates/',base="layout")
class Index(object):
def GET(self):
#this is use to"setup"the session with starting values
session.room=map.START
web.seeother("/game")
class GameEngine(object):
def GET(self):
if session.room:
return render.show_room(room=session.room)
else:
return render.you_died()
def POST(self):
form=web.input(action=None)
#there is a bug here,can you fix it?
if session.room and form.action:
session.room=session.room.go(form.action)
web.seeother("/game")
if __name__=="__main__":
app.run()
I got the error just as above. Also the project's path and the import gothonweb2 part are also above. I don't know why it returns 'No module named 'gothonweb2''. Can someone help me?
Related
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
The Following is my code hierarchy.
I am trying to create a package for my project, "UIGenerator" but it is giving me following error.
traceback (most recent call last):
File "run.py", line 1, in <module>
from UIGenerator import app
File "/Users/______/Desktop/UIGenerator/UIGenerator/__init__.py", line 2, in <module>
from site.routes import site
ModuleNotFoundError: No module named 'site.routes'; 'site' is not a package
Here is the code for run.py =>
from UIGenerator import app
#app.route('/')
def test_connection():
return "<h1>Connected<h1>"
if __name__ == '__main__':
app.run(debug=True)
There is nothing in site =>__init__.py file and in site=>routes.py, I have some sample code to test-
from flask import Blueprint
site = Blueprint('site', __name__)
#site.route('/site')
def site():
return "this is test for site package"
The following is my code for the UIGenerator's init.py file =>
from flask import Flask
from site.routes import site
def getApp():
app = Flask(__name__)
app.register_blueprint(site)
return app
app = getApp()
Can anyone give me any clue, please.
One way to fix this is to build a Python package with you files, like outlined here. This will be better in the long run if you want to scale this project up, and will allow you to declare things in init
That being said, I think you can get your setup working by moving this chunk of code
def getApp():
app = Flask(__name__)
app.register_blueprint(site)
return app
app = getApp()
to the run.py file for the time being.
So, here is what I found out from a different post. In my site=>routes, I declared a global blueprint with the name, site and later I created a site() function which somehow masked my site blueprint. If I just renamed the site method name, it works.
I was writing a little application where I wanted to create a module containg a small group of classes, but when I try to import the classes from the main application, I get the error:
my_project python3 main.py
1
Traceback (most recent call last):
File "main.py", line 2, in <module>
import receivers
File "/home/mario/Documents/python/my_project/receivers/__init__.py", line 2, in <module>
from icinga import Icinga
ModuleNotFoundError: No module named 'icinga'
The file in the project are:
├── main.py
└── receivers
├── icinga.py
├── __init__.py
where main.py
#!/usr/bin/env python
import receivers
icinga = receivers.icinga.Icinga()
the file receivers/icinga.py
class Icinga:
def __init__(self):
print("I'm Icinga!")
the file receivers/__init__.py
print('1')
from icinga import Icinga
print('2')
Can someone please tell me what I do wrong?
Thanks in advance
If you just want to import the Icinga class, you can do it as
from receivers.icinga import Icinga
If you want to call the import statement on receivers, you should alter the init.py, on 2nd line, to:
from .icinga import Icinga
I reproduced your problem here, and was able to solve it like that.
Edit:
Doing this second thing (on __init__.py), you would be able to call it on the main.py as:
import receivers
receivers.icinga.Icinga()
I am using Python 3 to try and get a test file for sample application working yet it keeps throwing ImportError: No module named 'calculate'
My file structure is:
/calculate
__init__.py
calculate.py
test/
__init__.py
calculate_test.py
I cannot figure out why this is the case, any help would be much appreciated.
The __init__.py files are empty.
calculate.py contains:
class Calculate(object):
def add(self, x, y):
return x + y
if __name__ == '__main__':
calc = Calculate()
result = calc.add(2, 2)
print(result)
calculate_test.py contains:
import unittest
from calculate import Calculate
class TestCalculate(unittest.TestCase):
def setUp(self):
self.calc = Calculate()
def test_add_method_returns_correct_result(self):
self.assertEqual(4, self.calc.add(2,2))
if __name__ == '__main__':
unittest.main()
I am running python test/calculate_test.py from the root /calculate folder and am getting the error
Traceback (most recent call last):
File "test/calculate_test.py", line 2, in <module>
from calculate import Calculate
ImportError: No module named 'calculate'
I have been fiddling around with different structures and cannot understand what the problem is.
Your project's structure is the reason. The test script doesn't have the outer directory in the search path when you start it. Here are some ways to fix that
Move the test file into the same directory that contains the module it imports. That will require no changes in the test file.
Use this structure
./project/
calculate_test.py
calculate/
__init__.py
calculate.py
This will require you to change the import signature in calculate_test.py to something like from calculate import calculate
I'm writing a Python package that looks like this:
|- datum/
|- __init__.py
|- database.py
__init__.py
from .database import Database
def connect(url):
print(Database) # for debugging
return Database(url)
database.py
class Database(object):
def __init__(self, url):
self.url = url
...more methods
This is installed as a package called datum in development mode. If I call connect outside of this package like:
import datum
db = datum.connect('postgresql://xxx')
...this is the output:
<class 'datum.database.Database'>
Traceback (most recent call last):
File "Z:\AIS\Flask\ais\engine\scripts\load_pwd_parcels.py", line 30, in <module>
source_db = Database(source_db_url)
NameError: name 'Database' is not defined
I'm confused because the class is being imported fine -- I can print it and even run dir on it and see all my methods -- but when I try to instantiate something it's "not defined". Does anyone know what I'm doing wrong here?
A NameError means that a local or global name cannot be found. I would make sure that your "load_pwd_parcels.py" file includes:
from database import Database
Alternatively, you could do:
import database
source_db = database.Database(source_db_url)
The latter option is a great choice because it gives database its very own namespace. In the words of Tim Peters in PEP 20 -- The Zen of Python:
"Namespaces are one honking great idea - let's do more of those!"