Python unittest fails to load modules when executed from command line - python

When I try to run python unittest from command line, in windows, I get the following error:
test_validateXml (unittest.loader.ModuleImportFailure) ... ERROR
consequently in the details there is an error message that reads:
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)
from application.helpers.fileHelper import FileIO as fileIO
ImportError: No module named application.helpers.fileHelper
The command I'm running is:
python -m unittest discover -v -p "*est*.py"
As I understand the reading I've done, all that's required in order to import modules is an __init__.py file in every directory of the project, which I have.
When I run the test from Visual Studio they run without any problems, any help is appreciated thanks.

Related

Visual Studio Code ModuleNotFoundError: No module named '_overlapped'

I have developed the following message when running flask. I attempted to add overlapped, via pip install overlap. I can't find answers to this. This is my terminal output. Can someone please help?
dvdjms#DESKTOP-OPG4GRH:/mnt/c/Users/dvdjm/Documents/CS50/project$ flask run
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.
Error: While importing 'app', an ImportError was raised:
Traceback (most recent call last):
File "/home/dvdjms/.local/lib/python3.8/site-packages/flask/cli.py", line 218, in locate_app
__import__(module_name)
File "/mnt/c/Users/dvdjm/Documents/CS50/project/app.py", line 1, in <module>
from asyncio.windows_events import NULL
File "/usr/lib/python3.8/asyncio/windows_events.py", line 3, in <module>
import _overlapped
ModuleNotFoundError: No module named '_overlapped'
Did you mean to import NULL from asyncio.windows_event? I'm guessing it was automatically imported by your IDE, None is likely what you're looking for instead of NULL, which isn't a keyword in Python.

For what reason would a Flask application run fine, but fail to load a module in the unittest?

So, I created a small flask API. Basically a wrapper for another API.
I used requests-futures to send multiple calls asynchronously.
I set up my virtual environment. Everything works exactly as I intend. However, when I try to run some tests I've written (using python -m unittest) this is the error I get:
======================================================================
ERROR: test (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test
Traceback (most recent call last):
File "/usr/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
__import__(name)
File "/home/cwverica/take-home-apps/quixr/test.py", line 2, in <module>
from app import app, session
File "/home/cwverica/take-home-apps/quixr/app.py", line 3, in <module>
from requests_futures.sessions import FuturesSession
ModuleNotFoundError: No module named 'requests_futures'
I have reinstalled the module using pip. I made sure to pip freeze > requirements.txt just in case it was reading module availability from there. But all in all I'm stumped. If anyone could help me out here, I'd greatly appreciate it.

Struggling with imports in Python

I'm just sruggling with importing modules from nested packages in Python.
After execute command in project root directory:
$ nosetests
Unfortunatelly, i'm still getting logs like this:
======================================================================
ERROR: Failure: ImportError (No module named io_file)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/nose/loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/home/user/dev/ease-ci/easeci-core/tests/lib/io/test_io_facade.py", line 4, in <module>
from lib.io.io_file import file_load, File, file_exist, file_save, file_delete, file_change
ImportError: No module named io_file
And more and more errors like that.
Can someone tell me something help me to resolve my issue? Thanks.
Pycharm put me in error, because if I run test by green arrow, everything is ok.
I'm guessing that you are trying to install a library without pip installed because you used pycharm in a weird way.
Try adding a path to the library which you have to download and then run the import within the code. Cheers!
import tensorflow as tf
https://www.jetbrains.com/help/pycharm/absolute-path-variables.html

How to run django test on google cloud platform

i'm deploying my app on Google Cloud platform. Every thing seems to be set up correctly but when i try to launch test, i get this error :
======================================================================
ERROR: purbeurre.core (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: purbeurre.core
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/loader.py", line 462, in _find_test_path
package = self._get_module_from_name(name)
File "/usr/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
ModuleNotFoundError: No module named 'purbeurre.core'
Is there a special setting to have all my test modules loaded ?
i did check locally and it's working on my dev environnement
It appear that i have an init.py file at root level of my project and it's messing up with unittest.
After deleting this init.py file the test is launched properly

PyCharm: importing cython module that uses dynamic libraries itself - ImportError & Code Completion

I've got a cython module that is a wrapper around a c++ library that communicates with a database. That library is called libpersistence.so. That library itself is dynamically linked with libhiredis.so.
I wrote a cython wrapper around the libpersistence.so to create a py_persistence.cpython-35m-x86_64-linux-gnu.so file to import as a module in my python scripts to use the cpp persistence layer.
However, upon importing the py_persistence module there's two issues in PyCharm:
Importing the Persistence class from the module does not get recognized by PyCharm. This also means there is no code completing
Running my tests leads to an ImportError where the libpersistence.so file shared object file can not be opened.
I use the following directory structure.
top_level/
indexing_service/
bin/
libhiredis.so.0.13
libpersistence.so
kCore/
include/
hiredis/
persistence/
query_service/
__init__
runpython.sh
py_persistence/
__init__
py_persistence.cpython-35m-x86_64-linux-gnu.so
py_persistence.pxd
py_persistence.pyx
setup.py
setup.sh
test/
test_persistence.py
The setup.sh file just specifies python3 setup.py build_ext --inplace
The setup.py file is the following:
from distutils.core import setup
from Cython.Distutils import build_ext, Extension
setup(
cmdclass = { 'build_ext' : build_ext },
ext_modules = [
Extension(
'py_persistence',
sources=['py_persistence.pyx'],
libraries=['persistence'],
language='c++',
extra_link_args=['-L../../indexing_service/bin/'],
runtime_library_dirs=['../../indexing_service/bin/'],
extra_compile_args=[
'-std=c++14',
'-I../../indexing_service/kCore/include/persistence',
'-I../../indexing_service/kCore/include/hiredis'
],
cython_directives={
'c_string_type':'unicode',
'c_string_encoding':'utf8',
},
)],
)
So when building the cython py_persistence library it links to the both the libpersistence.so and the libhiredis.so.0.13 files for dynamic linking. This works, this compiles and it builds the py_persistence.cpython-35m-x86_64-linux-gnu.so file correctly.
When I run my tests using the runpython.py helper script I wrote everything works perfectly fine.
The runpython.py explicitely adds the required directories to the runtime library path:
export PYTHONPATH=$PYTHONPATH:.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../indexing_service/bin/:.
python3 ${#:1}
Running the tests:
./runpython.sh -m unittest discover py_persistence/test -v
This works perfectly.
However getting this setup working so that I can run it from PyCharm is giving me a lot of trouble. I don't seem to be able to correctly specfify the paths. In the test_persistence.py file I get an ImportError while importing the py_persistence library.
Import code:
from py_persistence.py_persistence import Persistence
The error:
Error
Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor
yield
File "/usr/lib/python3.5/unittest/case.py", line 600, in run
testMethod()
File "/usr/lib/python3.5/unittest/loader.py", line 34, in testFailure
raise self._exception
ImportError: Failed to import test module: test_persistence
Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/loader.py", line 428, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/lib/python3.5/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
File "/home/tom/work/dev/alexandria.works/query_service/py_persistence/test/test_persistence.py", line 3, in <module>
from py_persistence.py_persistence import Persistence
ImportError: libpersistence.so: cannot open shared object file: No such file or directory
I've already tried adding the indexing_service/bin/ folder to the path in PyCharm for the virtual environment:
Interpreter Paths in PyCharm
The run configuration used to run the tests is the following:
Run Configuration
The problems I'm experiencing are the ImportError and the fact that code completion isn't working.
I'm at a loss about what else to try here... Printing sys.path in the test_persistence.py file tells me that the indexing_service/bin/ folder is on the path.
['/home/tom/Applications/pycharm-2017.1.1/helpers/pycharm',
'/home/tom/work/dev/alexandria.works/query_service/py_persistence/test',
'/home/tom/work/dev/alexandria.works/indexing_service/bin',
'/home/tom/work/dev/alexandria.works/query_service',
'/home/tom/Applications/pycharm-2017.1.1/helpers/pycharm',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python35.zip',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5/plat-x86_64-linux-gnu',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5/lib-dynload', '/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5/site-packages']
Anybody know what I missed?

Categories

Resources