coverage cannot find modules in my project - python

I am trying to use coverage from the terminal (as I am using the community version of pycharm, which doesn't have support for coverage). However, I always get the same error:
ModuleNotFoundError: No module named 'src' The package structure of my project is very simple: it has 2 packages, src and test, and inside the test package is another package called unit, wherer I keep my unit tests.
This is the code that I have trying to run with coverage.
import unittest
from src.interpolacion import interpolacion
class DummyTest(unittest.TestCase):
def test_nothing(self):
self.assertTrue(True)
if __name__ == '__main__':
unittest.main()
Please notice how src.interpolacion.interpolacion is not even used and, regardless, it fails with this error:
======================================================================
ERROR: DummyTest (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: DummyTest
Traceback (most recent call last):
File "C:\Users\jose.m.ramirez.leon\AppData\Local\Programs\Python\Python38\lib\unittest\loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
File "C:\Users\jose.m.ramirez.leon\PycharmProjects\dummyPythonProject\test\unit\DummyTest.py", line 2, in <module>
from src.interpolacion import interpolacion
ModuleNotFoundError: No module named 'src'
I am going mad with this. Could anyone please help me regain my sanity?

Related

Importing modules in embedded python

I'm trying to get module imports to work in embeddable python, but it doesn't want to work
C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>type run_scripts\script.py
from module_test import test
print("Hello world!")
print(test())
C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>type run_scripts\module_test.py
def test():
return "Test!"
C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>#python.exe run_scripts\script.py
Traceback (most recent call last):
File "C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32\run_scripts\script.py", line 1, in <module>
from module_test import test
ModuleNotFoundError: No module named 'module_test'
Why is the module not being imported? I tried changing PYTHONPATH but it didn't help

ModuleNotFoundError: No module named 'kucoin.client'

I am getting the error: "ModuleNotFoundError: No module named 'kucoin.client'; 'kucoin' is not a package" when running the code underneath. I did pip install like in the documentation here: hhttps://python-kucoin.readthedocs.io/en/latest/ . What is going wrong?
import api_KuCoin
Xkey = api_KuCoin.Pkey
Ykey = api_KuCoin.Skey
Zkey = api_KuCoin.Dkey
client = Client(api_key=Xkey, api_secret=Ykey, api_passphrase=Zkey)```
Traceback (most recent call last):
File "d:\Crypto\kucooin.py", line 1, in <module>
from kucoin.client import Client
ModuleNotFoundError: No module named 'kucoin.client'
This error: ModuleNotFoundError: No module named 'kucoin.client'; 'kucoin' is not a package may also occur if you have named the main program file you created as kucoin.py and try to run it as python kucoin.py or another file has the name kucoin.py in the same folder from which you run your program. Python will consider your program file as a module and try to find something in it that is naturally not in it. About where Python is looking for modules, see sys.path.
In this case, rename your program file so that its name does not equal with the name of the imported module.

Relative import error using python unittest

I'm using the built-in python unittest module. My directory structure is as follows:
game.py
test/test_game.py
The content of test_game.py:
import unittest
from ..game import *
class TestGame(unittest.TestCase):
def test_pawn(self):
game = Game()
game.make_move("e2e3")
self.assertEqual(game.board[6][5].piece_type, "P")
if __name__ == '__main__':
unittest.main()
Here's the problem. When I run python -m unittest test/test_game.py I get the error:
ImportError: Failed to import test module: test_game
Traceback (most recent call last):
File "/usr/local/Cellar/python#3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
ModuleNotFoundError: No module named 'test.test_game'
I was missing the test/__init__.py file. With this change, I didn't even need the relative ..game import.. But I'm wondering why python requires this just to import

What are the reserved Python module/package names?

I got a strange error while working with Python unittest. I have two folders in my project:
project
code
__init__.py (empty)
app.py (defines my App class)
test
test.py (contains my unit tests)
test.py is:
import os, sys, unittest
sys.path.insert(1, os.path.join(sys.path[0],'..'))
from code.app import App
class test_func1(unittest.TestCase):
...
When I run test.py I get the message:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "...test.py, line 5, in <module>
from code.app import App
ImportError: No module named 'code.app': 'code' is not a package
After verifying that __init__.py was present and banging my head for a while, on a whim I changed the name of the app directory from code to prog:
import os, sys, unittest
sys.path.insert(1, os.path.join(sys.path[0],'..'))
from prog.app import App
... and everything was suddenly fine. Unittest imported my app properly and ran the tests.
I've searched through https://docs.python.org/3.5/reference/lexical_analysis.html#keywords and https://docs.python.org/3/reference/import.html#path-entry-finders and don't see any indication that code is an illegal directory name. Where would this be documented, and what other directory names are reserved?
System: python 3.4.3 [MSC v1600 32 bit] on win32, Windows 7
code isn't reserved, but it is already defined in the standard library, where is it a regular module and not package. To import from your package, you should use a relative import.
from .code.app import App

importing a file as a package - Python

I have the following directory structure:
/testlib
__init__.py
ec2.py
unit/
__init__.py
test_ec2.py
utils/
__init__.py
I'm trying to create a unittest class for ec2.py:
import ec2
class TestEC2(unittest.TestCase):
def setUp(self):
self.ec2obj = ec2.EC2(name="testlib_unittest")
if __name__ == '__main__':
unittest.main()
However, when I execute test_ec2.py I'm getting the following error:
python unit/test_ec2.py
Traceback (most recent call last):
File "unit/test_ec2.py", line 4, in <module>
import ec2
ImportError: No module named ec2
I still don't understand why I'm getting that, since I have the __init__.py properly set in the directory. The __init__.py files are completely empty: I've created them with touch __init__.py. Any clues?
**** Updates ****
Here are some suggestions:
/testlib# python unit/test_ec2.py
Traceback (most recent call last):
File "unit/test_ec2.py", line 4, in <module>
from ..ec2 import EC2
ValueError: Attempted relative import in non-package
testlib# python unit/test_ec2.py
Traceback (most recent call last):
File "unit/test_ec2.py", line 4, in <module>
import testlib.ec2 as ec2
ImportError: No module named testlib.ec2
It can't find the module because you're running the script incorrectly. Run the following in testlib/:
python -m unit.test_ec2
Python is not looking for files in directories above yours. Ignacio's answer is correct, and see this for elaboration.

Categories