Import file from parent directory when using PyTest? - python

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()

Related

Pytest how to test a script1 that imports script2

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)

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 import relative module from __main__ in python

I have two file
foo.py
main.py
while executing python main.py, I want main.py to import foo.py.
I tried
from . import foo
But python complains
Attempted relative import beyond top-level package
I also tried
from foo import func_name
It works, but that means I can only directly import what get exposed. I want to use it like foo.func_name
Is it possible to import the whole module in a relative way (regardless what the current working directory is)?
The script directory is already a part of the import path. Simply use
import foo

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