Pytest how to test a script1 that imports script2 - python

I have following project tree:
/root
/tests
test_dummy.py
/tools
script0.py
/src
script1.py
script2.py
/...
test_dummy.py
import sys
import script0
from src import script1
...
script1.py
import script2
...
I call pytest from bash and before I call export PYTHONPATH="$(pwd)/tools"
The problem is, importing script1.py doesn't work since it tries to locally import script2 which as I understood is not visible in sys.path.
import script1
E ModuleNotFoundError: No module named 'script1'
How to be able to import a script that imports another script file and test it?
import script0 works fine
from src import script2 also works fine
Trying to just specify import path directly worked but it looks ugly to repeat it if there are different directories that have to be tested
//sys.path[1] has a root path
testing_script_path="<src>"
sys.path.insert(0,sys.path[1]+'/'+testing_script_path)

Related

Module not found on imported module that shares module with the script

Hello I have this problem on Python where I need to call a function from a script but when I imported it throws me an error of No module name '...'
My root folder looks like
Proyect
|_main.py
|_folder
|_folder2
|_script1.py
|_script2.py
In main.py I import script1.py as
import folder.folder2.script1 as tools
script2.py also import script1.py as
import script1 as tools
All this run without problems but if I call script2 as
from folder.folder2.script2 import foo
It returns me an error saying that it can not find script1 in script2
Hope all of this make any sense
Edit: Seems like script2.py tries to import script1 from the folder where is main.py but can not confirm if this is a normal behavior

ModuleNotFoundError: Even with __init__.py file

I have 3 python file :
test.py
modules(folder) and in modules there 3 to files : module1.py module2.py init.py
test.py
./test.py
./modules
./modules/module1.py
./modules/module2.py
./modules/__init__.py
module1.py:
from module2 import temp
def print_temp():
print(temp)
if __name__=='__main__':
print_temp()
module2.py
temp =1
test.py
from modules.module1 import print_temp
print_temp()
When I run python test.py I got ModuleNotFoundError: No module named 'module2'
How can I fix that please?
When you import a module in Python, it searches the PYTHONPATH for a module / package with that name. But it doesn't search inside directories so you have to specify.
If you want to be able to run modules.module1 directly (as well as import it) then you must use the full path:
from modules.module2 import temp
But if you just want to be able to import the module and not run it directly it is better to use a relative import as it will still work even if the name of the package is changed:
from .module2 import temp

How to use Python imports between scripts in the same folder, imported as part of a package, and run as a stand-alone script within the folder

I have been reading through absolute vs relative imports for a long time and nothing has helped me.
Say I have a python module structured like so:
└── test_project
├── __init__.py
├── script1.py
├── script2.py
└── main.py
__init__.py is blank
script1.py has functions
script2.py accesses script1. This can be achieved through from . import script1,import .script1 (relative) or import script1 (absolute)
main.py is a script that runs a function from script1 on the command line, and therefore includes if '__name__'==__main__: followed by some version of import script1
I want to be able to import script2 into a python terminal both outside and inside the folder, and run the main.py script on the command line from both outside and inside the folder i.e. python test_project/main.py.
Despite a lot of trying, I cannot find any way of writing the import commands such that import script2 works in all these cases.
For example, if I use from . import script1 in main and script2 and run from test_project import script2 in python from outside the folder, it works fine. But if I run python test_project/main.py or try to import in python when within the folder, I get:
Traceback (most recent call last):
File "test_project/main.py", line 1, in <module>
from . import script1
ImportError: attempted relative import with no known parent package
I can try to modify the imports to be absolute to fix this. For example making main and script2 have import script1 rather than being relative. Now running main and importing from a python within the folder is ok, but importing from outside the folder with from test_project import script2 gives:
<ipython-input-1-c3cdbe7a7496> in <module>
----> 1 from test_project import script2
~/python/test_project/script2.py in <module>
----> 1 import script1
ModuleNotFoundError: No module named 'script1'
Is it, therefore, impossible to write import statements in such a way that these functionalities are possible?

Import file from parent directory when using PyTest?

I am using PyTest, but it can't seem to run my import statements.
My directory structures is like this:
-[app]
-script1.py
-[tests]
-test1.py
My test1.py file includes this line:
from script1 import my_func
and I get an error like ModuleNotFoundError: No module named script1 when I run pytest from the command line.
I tried adding this statement to cd the directory above but it didn't work:
import os
os.chdir('..')
Like you specified. script1.py module is in [app]
So it should be
from app.script1 import my_func
or if you want to import all functions from script.py
import app.script1
and just call any function
script1.anyfunction()

Unable to import module from parent directory in package

I have the following the directory structure
pytest_testing/
__init__.py
math_ops.py
dbs/
__init__.py
dbConnect.py
tests/
test1.py
Now in test1.py i am trying to do import a function from dbConnect module, like this from pytest_testing.dbs.dbConnect import query_data but i get error "No Module named pytest_testin.
If i try the same in a directory above pytest_testing everything works just fine. Am i missing something here ?
Well, you can just type this from dbs.dbConnect import query_data.
I think this can work well
If you are using Linux or Windows use the code:
# test1.py
import sys
sys.path.append(/path/to/pytest_testing/dbs/)
from dbConnect import functionName #dbConnect without .py extension

Categories