I create a notebook which looks like the following:
import unittest
class TestHelpers(unittest.TestCase):
def test_trivial2(self):
self.assertEqual(True,True)
unittest.main(argv=[''],exit=False)
When running the notebook, everything works fine. Now, I try to move the test out to a separate *.py file:
Notebook code:
import unittest
unittest.main(argv=[''],exit=False)
New Python file (in same repo):
import unittest
class TestHelpers(unittest.TestCase):
def test_trivial2(self):
self.assertEqual(True,True)
When running the notebook I always get
Ran 0 test
I tried to add the path as argv such as
unittest.main(argv=['/Workspace/Repos/XXX/repo'],exit=False)
but the *.py file is not discorved
What do I have to hand over to argv so that the py file is identified
Thank you Daniil for the link:
import unittest
unittest.main(module="simple_test",argv=[''],exit=False)
makes the job!
Related
I have the following working directory with my source and tests folders as illustrated
root_folder:
tower_shell
init
read_files
files_processing
tests
init
test_read_json.py
I want to run the unit test test_read_json.py from my folder tests which need the following imports:
import unittest
import os
import sys
from tower_shell.read_files import read_json
from pathlib import Path
The problem is that the file read_files (contained in tower_shell) folder is also importing a module from tower_shell called files_processing: \
from files_processing import process_hub_sheet, process_tower_sheet
Since my working directory is one level up, it raises an error stating that the module can not be imported.
I tried to add PYTHONPATH variable but the test discovers is not functioning then.
Thanks
I am working on a jupyter notebook in AWS, I have two files: main.ipynb and utils.py, what I would like to do is to import utils in my jupyter notebook file.
Unfortunately I have tried the following solutions and none of them are working:
import sys
sys.path.append("/home/jovyan/dir1")
%load utils.py
And to directly import after changing the directory
import utils
my file "utils.py":
def hello():
print("hello")
Problem solved:
the solution is to add those :
s3 = boto3.resource('s3')
s3.meta.client.download_file(os.environ['S3_BUCKET'], "dir1/utils.py", "utils.py")
import utils
Export python path to the path from where you are trying to import utils.
Run this below command from you file path
export $PYTHONPATH=.
if I have written a unittest, that passes, if I run my tests.py in IDE Pycharm. If I use the console (in unix) however and start the unittest with:
python3 tests/tests.py
I get the following error:
ModuleNotFoundError: No module named 'helpermodule'
My folder structure looks like this:
folder structure
The files look like the following:
Mytest.py:
def add(x,y):
return x+y
tests.py:
from helpermodule import Mytest
import unittest
What am I missing here? I have read several posts concerning the problem, but I could not find a solution that worked.
If anyone has the same problem, the simple solution is to call the test.py with right parameters:
python3 -m unittest tests/tests.py
see also:
https://docs.python.org/3/library/unittest.html#command-line-interface
I have the following code in myfile.py
def show_path():
print(os.getcwd())
In Jupyter notebook, I have the following (which runs fine):
import os
from myfile.py import show_path
However, when I run the following:
show_path()
I get 'name 'os' is not defined' error. But when I simply type:
os.getcwd()
I get the path, which I understand. But I don't understand why running show_path() doesn't do the same thing? Is it necessary to have import os inside my myfile.py file? If so, why?
You have to import os in your module, its import list is different then that of your notebook. Since you did not import it in your module, it wa unavailable when your module was parsed.
I use JupyterLab, jupyter notebook and try to import the script. The script is located in the notebook's directory.
Unfortunately import is impossible.
Error:
ImportError: No module named 'irt_01_generate_sample_data'
On the other hand:
When I run terminal and import the script, everything works fine.
What should I do to import the script ?
One thing to check is whether you're running the same Python (executable) and have the same paths (where it is looking for imports) in both the terminal and Jupyter Notebook:
import sys
sys.executable
sys.path