Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot import module from a folder

I'm using VSCODE as my editor and using Python 3.7 I have this kind of folder structure

project
|-- util
   | -- drivers.py
   | -- data.py
   | -- __init__.py
|-- test
   | -- driver_test.py
   | -- __init__.py
main.py

I want to import drivers.py from util folder into driver_test.py I tried to do basic import like this

from util.drivers import Driver

which didn't work since it couldn't find util module, then I used another approach by adding a folder to sys.path. like this

# driver_test.py
sys.path.append(os.path.abspath('./util'))
import drivers

I got very confused with several output errors. When I run my unittest from console I'm getting error that drivers are not found

import drivers
ModuleNotFoundError: No module named 'drivers'

And if i run test file in vscode editor by right click and Run current test file, the error output is different that selenium driver is not found which is included inside drivers.py file

from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'

Selenium module is actually working fine and I just wanted to right several unittest for it and most. What I'm doing wrong and how can this be solved? And is it possible to include a module without using sys.path just by using import.

like image 375
Andrew Avatar asked Nov 06 '22 16:11

Andrew


1 Answers

You might want to set your PYTHONPATH, to the project directory. More on that in this discussion on the StackOverflow forums or on the official Python website.

like image 94
peki Avatar answered Nov 13 '22 00:11

peki