Coverage : ImportError: cannot import name config - python

When I try to run this code I am getting: ImportError: cannot import name config in line 32 of this file:
lib/python2.7/site-packages/tests/functional_tests/fixtures/__init__.py
Any idea about the reason of this error?
import os
COV = None
if os.environ.get('FLASK_COVERAGE'):
import coverage
COV = coverage.coverage(branch=True, include='app/*')
COV.start()
from flask.ext.script import Manager, Shell
from app import create_app, db
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
#manager.command
def test(coverage=False):
"""Run the unit tests."""
if coverage and not os.environ.get('FLASK_COVERAGE'):
import sys
os.environ['FLASK_COVERAGE'] = '1'
os.execvp(sys.executable, [sys.executable] + sys.argv)
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
if COV:
COV.stop()
COV.save()
print('Coverage Summary:')
COV.report()
basedir = os.path.abspath(os.path.dirname(__file__))
covdir = os.path.join(basedir, 'tmp/coverage')
COV.html_report(directory=covdir)
print('HTML version: file://%s/index.html' % covdir)
COV.erase()
if __name__ == '__main__':
manager.run()
error log:
python coverage.py test
ERROR: tests.functional_tests.test_providers (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.functional_tests.test_providers
Traceback (most recent call last):
File "/usr/lib/python2.7/unittest/loader.py", line 252, in _find_tests
module = self._get_module_from_name(name)
File "/usr/lib/python2.7/unittest/loader.py", line 230, in _get_module_from_name
__import__(name)
File "/home/vagrant/myproject/venv/local/lib/python2.7/site-packages/tests/functional_tests/__init__.py", line 1, in <module>
from . import fixtures
File "/home/vagrant/myproject/venv/local/lib/python2.7/site-packages/tests/functional_tests/fixtures/__init__.py", line 32, in <module>
from tests.functional_tests import config
ImportError: cannot import name config
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)

Related

Getting multiple error while using flask-ask and ngrok with ROS

I'm trying to use flask-ask library and develope a skill that can make me able to control a robot in ROS with alexa vocal assistant. Python code named "alexa_interface.py" is here.
The skill id number is substituted with my skill id number.
While launching this command
python3 alexa_interface.py
I am getting the following code error.
Tried to install dependencies with pip but getting:
AttributeError: module 'lib' has no attribute 'ERR_load_RAND_strings'
Also notice that I am getting this error everytime I use pip to install something. Is pip broken? How can I adjust this?
Thank you.
#! /usr/bin/env python3
from flask import Flask
from ask_sdk_core.skill_builder import SkillBuilder
from flask_ask_sdk.skill_adapter import SkillAdapter
from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_core.utils import is_request_type, is_intent_name
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model import Response
from ask_sdk_model.ui import SimpleCard
app = Flask(__name__)
class LaunchRequestHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_request_type("LaunchRequest")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
speech_text = "Ciao, come posso aiutarti?"
handler_input.response_builder.speak(speech_text).set_card(
SimpleCard("Hello World", speech_text)).set_should_end_session(
False)
return handler_input.response_builder.response`
skill_builder = SkillBuilder()
# Register your intent handlers to the skill_builder object
skill_builder.add_request_handler(LaunchRequestHandler())
skill_adapter = SkillAdapter(
skill=skill_builder.create(), skill_id="my-skill-id", app=app)
skill_adapter.register(app=app, route="/")
if __name__=='__main__':
app.run()
```
```
Traceback (most recent call last):
File "alexa_interface.py", line 2, in <module>
from flask import Flask
File "/home/toti/.local/lib/python3.8/site-packages/flask/__init__.py", line 14, in <module>
from jinja2 import escape
ImportError: cannot import name 'escape' from 'jinja2' (/home/toti/.local/lib/python3.8/site-packages/jinja2/__init__.py)
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 32, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 12, in <module>
import os, glob, subprocess, os.path, time, pwd, sys, requests_unixsocket
File "/usr/lib/python3/dist-packages/requests_unixsocket/__init__.py", line 1, in <module>
import requests
File "/home/toti/.local/lib/python3.8/site-packages/requests/__init__.py", line 52, in <module>
from .packages.urllib3.contrib import pyopenssl
File "/home/toti/.local/lib/python3.8/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 46, in <module>
import OpenSSL.SSL
File "/home/toti/.local/lib/python3.8/site-packages/OpenSSL/__init__.py", line 8, in <module>
from OpenSSL import rand, crypto, SSL
File "/home/toti/.local/lib/python3.8/site-packages/OpenSSL/rand.py", line 213, in <module>
_lib.ERR_load_RAND_strings()
AttributeError: module 'lib' has no attribute 'ERR_load_RAND_strings'
Original exception was:
Traceback (most recent call last):
File "alexa_interface.py", line 2, in <module>
from flask import Flask
File "/home/toti/.local/lib/python3.8/site-packages/flask/__init__.py", line 14, in <module>
from jinja2 import escape
ImportError: cannot import name 'escape' from 'jinja2' (/home/toti/.local/lib/python3.8/site-packages/jinja2/__init__.py)
```

Importing a python module from another directory that contains other imports

Assuming I have this structure below :
main1.py :
import sys
sys.path.insert(0, '../')
from projectB import main2
if __name__ == "__main__":
main2.main()
main2.py :
import module2
def main():
print("test1")
module2.print_2()
if __name__ == "__main__":
main()
module2.py:
def print_2():
print("test2")
When executing main1.py I get this error below :
Traceback (most recent call last):
File "/projectA/main1.py", line 4, in <module>
from projectB import main2
File "/projectA/../projectB/main2.py", line 1, in <module>
import module2
ModuleNotFoundError: No module named 'module2'
How do I solve this issue ?

Flask Error When I run python program in pycharme

i have this error after run:
Traceback (most recent call last):
File "/home/user/PycharmProjects/third_project/flask_test.py", line 1, in <module>
from flask import Flask
File "/home/user/PycharmProjects/third_project/flask.py", line 1, in <module>
from flask import Fl
ImportError: cannot import name 'Fl' from partially initialized module 'flask' (most likely due to a circular import) (/home/user/PycharmProjects/third_project/flask.py)
Process finished with exit code 1
************************ the code :flask_test.py**************************
from flask import Flask
skills_app = Flask(__name__)
#skills_app.route("/")
def hompage():
return "hello flask"
#skills_app.route("/about")
def about():
return "hello flask/about"
if __name__=="__main__":
skills_app.run(debug=True)

Django Test unittest.loader.ModuleImportFailure

I can't import my models in my tests directory, this is my error :
======================================================================
ERROR: tests.test_views (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.test_views
Traceback (most recent call last):
File "C:\Python27\lib\unittest\loader.py", line 254, in _find_tests
module = self._get_module_from_name(name)
File "C:\Python27\lib\unittest\loader.py", line 232, in _get_module_from_name
__import__(name)
File "c:\wamp\www\km0\tests\test_views.py", line 3, in <module>
from .models import Entreprise
File "c:\wamp\www\km0\tests\models.py", line 6, in <module>
class Entreprise(models.Model):
File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\ba
se.py", line 102, in __new__
"INSTALLED_APPS." % (module, name)
RuntimeError: Model class tests.models.Entreprise doesn't declare an explicit ap
p_label and isn't in an application in INSTALLED_APPS.
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
Preserving test database for alias 'default' ('test_km0')...
I did some research and couldn't find an answer ...
test_views.py :
from django.test import TestCase
import unittest
from .models import Entreprise
class Km0ViewsTestCase(TestCase):
def test_cart(self):
resp = self.client.get('/fr/cart/')
self.assertEqual(resp.status_code, 200)
resp = self.client.get('/en/cart/')
self.assertEqual(resp.status_code, 200)
resp = self.client.get('/de/cart/')
self.assertEqual(resp.status_code, 200)
My directory :
my directory
Thanks in advance for your help !
You shouldn't normally need a models.py in your tests directory.
If you want to import your Entreprise model from the front app, then change your import from
from .models import Entreprise
to
from front.models import Entreprise

Python Nosetest - How to mock MySQLdb

I'm trying to mock the MySQLdb lib from my project but I don't really understood how to do it:
Here's my test code:
import unittest
from mock import MagicMock
from core.distrib.main.CentralDistDraw import CentralDistDraw
from connector.mysql import MySQL
class CentralDistDraw(unittest.TestCase):
def test_hello(self):
draw = CentralDistDraw()
msql = MySQL()
msql.getConnection = MagicMock()
Here's my class:
from datetime import datetime
import logging
from core.distrib.config.PromoConstants import PromoConstants
from dateutil.parser import parse
from google.appengine.api import memcache
from connector.mysql import MySQL
from core.distrib.ndb.userpromo import UserPromo
from core.distrib.ndb.drawinfo import DrawInfo
import traceback
class CentralDistDraw(object):
def getNextNumber(self, idUser, amountDefault=1, amountSpecial=1):
numbersDelivered = []
# DB CONTROLL....
db = MySQL().getConnection()
db.autocommit(False)
currentUser = None
Error thrown:
ERROR: Failure: ImportError (/home/dyego/Documents/code-stuff/mod/pdbcontest2/src/lib/_mysql.so: invalid ELF header)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/nose-1.3.7-py2.7.egg/nose/loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "/usr/local/lib/python2.7/dist-packages/nose-1.3.7-py2.7.egg/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/usr/local/lib/python2.7/dist-packages/nose-1.3.7-py2.7.egg/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/home/dyego/Documents/code-stuff/mod/pdbcontest2/src/tests/core/test_central_dist_draw.py", line 4, in <module>
from core.distrib.main.CentralDistDraw import CentralDistDraw
File "/home/dyego/Documents/code-stuff/mod/pdbcontest2/src/core/distrib/main/CentralDistDraw.py", line 11, in <module>
from connector.mysql import MySQL
File "/home/dyego/Documents/code-stuff/mod/pdbcontest2/src/connector/mysql.py", line 8, in <module>
import MySQLdb
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: /home/dyego/Documents/code-stuff/mod/pdbcontest2/src/lib/_mysql.so: invalid ELF header
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
Anyone knows the correct way to go when comes to testing third party libraries?
Thanks!

Categories