Visual Studio Code Unit Tests Environment - python-3.x

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

Related

Import modules for testing with Pytest

I'm trying to test my code with Pytest framework, but for some reason I don't succeed importing my own code...
You have to import your code from the top module, like this:
from se2pdf.se2pdf import changeFileExtensionToPDF
the first se2pdf is the folder name and the second your file name. If it doesn't work, make sure there is a _init_.py file in the modules.

Importing Module issue (name of project and name of module are the same)

So I am having an issue trying to import some modules right now because the name of the project is same as the module name. At least thats what I think.
I am trying to run the test_aws_file.py main file. That file is trying to import my.test.utils.util_file, but it is getting this error:
ModuleNotFoundError: No module named 'my.test.aws', so seems like i can import up to 'my.test' then when i add .aws then it would cause the error
test_aws_file.py import coding:
sys.path.insert(1, os.path.abspath('.'))
print(sys.path)
from my.test.aws.utils.utils_file import UtilsFile
project name (i do have a init.py in all directories): my.test.aws
my/
test/
aws/
utils/
util_file.py
test_aws/
test_aws_file.py
you should have __init__.py files in your directories to make those packages, otherwise the import wont work.

using class from different directory

[Python 3.5.2, Docker container]
I have a class BCM in a file called metrics.py which I would like to use in several Jupyter notebooks which are in different directories. When they are in the same file, the following obviously works from metrics import BCM. I would like it to work with the following directory structure.
experiments\
common\
__init__.py
metrics.py
exp1\
glm.ipynb
exp2\
gbm.ipynb
It works if I do the following
import os, sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
sys.path.append(module_path)
from common.metrics import BCM
Based on posts that I have seen here, it would seem that the following should work from ..common.metrics import BCM. I get the following error SystemError: Parent module '' not loaded, cannot perform relative import.
Is there a way to use that class without changing the path as shown above?

How can I import a module from a parent directory?

I have the following directory structure for my Python project:
lib
__init__.py
tasks.py
api(directory)
__init__.py
app.py
Now, I want to import the tasks module into app.py. If I just type
import tasks
it magically works in Pycharm, but when executed via bash I get an error message saying "No module named tasks". I also tried
from .. import tasks
which gives me
ValueError: attempted relative import beyond top-level package
What am I doing wrong? Why does import tasks work when executed in Pycharm?

A test running with nosetests fails with ImportError, but works with python command

When run a test with python mycore/tests4extractor.py it works. If run the test with nosetests ./mycore/tests4extractor.py it fails with ImportError: No module named extractor. I am in the helpers folder.
The project structure is:
helpers/
mycore/
__init__.py
extractor.py
tests4extractor.py
Setting PYTHONPATH to the absolute path to helpers and/or helpers/mycore doesn't help.
Answer
tests4extractor.py:
import mycore
from extractor import extract
should be changed to:
import mycore
from mycore.extractor import extract
And python should be run with python -mmycore.tests4_strings
Does tests4extractor.py contain import extractor?
Because mycore is a package, you need to use absolute imports:
from mycore import extractor
or relative imports:
from . import extractor

Resources