1

I basically want to import the function videotoimage from the file videotoimage.py. When I import the function videotoimage (inside videotoimage.py) from main.py by:

from preprocessing.videotoimage import *

videotoimage(dir = "C:\\Users\\{user}\\Desktop\\Deepfake Datasets\\test")

I get an error saying:

Traceback (most recent call last):
  File "c:\Users\{user}\Desktop\deepfakedetectML\preprocessing\main.py", line 20, in <module>
    from preprocessing.videotoimage import *
ModuleNotFoundError: No module named 'preprocessing'

The project tree is:

C:.
│   CODE_OF_CONDUCT.md
│   LICENSE
│   README.md
│   requirements.txt
│
├───.github
│   │   CODEOWNERS
│   │
│   └───ISSUE_TEMPLATE
│           bug_report.yml
│           documentation.yml
│           feature_request.yml
│
└───preprocessing
        extractfaces.py
        main.py
        videotoimage.py

The code inside videotoimage.py is: https://privatebin.net/?9c10407fd482752d#AHcNVb12VjquQJEmdJKZEmor8amAGuAkVp3HNhnKHSHC

  • Your script is in the same folder as videotoimage.py. Just `import videotoimage` – Amith Lakkakula Aug 18 '21 at 11:01
  • which python version are you using? If i remember correctly, in older versions is was necessary to place a `__init__.py` into `preprocessing` in order to `import preprocessing.videotoimage` from withing a module in `preprocessing`. – H. Doebler Aug 18 '21 at 11:02
  • Do you have a `__init__.py` in your `preprocessing` module? See [the docs](https://docs.python.org/3/tutorial/modules.html#packages) for information on packaging and modules. – Alex Aug 18 '21 at 11:02
  • `main.py` and `videotoimage.py` are in the same folder so you can do `from videotoimage` . To use `from preprocessing.videotoimage` you would have to add `c:\Users\{user}\Desktop\deepfakedetectML` to `sys.path` before importing – furas Aug 18 '21 at 11:02
  • You may wish to follow a packaging tutorial (such as [this one](https://python-packaging-tutorial.readthedocs.io/en/latest/setup_py.html), with the use of a `setup.py` file) to turn your project into a proper python package so you can actually name your top level import `preprocessing`. – metatoaster Aug 18 '21 at 11:03
  • @H.Doebler, I'm using Python 3.9.6 –  Aug 18 '21 at 11:05
  • @AmithLakkakula, I tried it, I get a `TypeError: 'module' object is not callable` –  Aug 18 '21 at 11:06
  • @Alex, I tried following your steps and made an `__init__.py` with the neccessary imports inside it, but it didn't fix anything, I got the same error –  Aug 18 '21 at 11:07
  • @user69420 the `__init__.py` should be empty – Alex Aug 18 '21 at 11:07
  • @furas, I'm not trying to make a python library, I'm just trying to import stuff –  Aug 18 '21 at 11:08
  • @Alex, I tried the selected solution and it fixed everything. anyway thanks for your help –  Aug 18 '21 at 11:10
  • I think with an `__init__.py` he can `import preprocessing.videotoimage` only from a module next to the `preprocessing` directory. Within that directory such imports are only possible if `preprocessing` is an installed package (eg in a venv using `pip install . -e`). Or using `sys.path` tweaking, but you certainly dont want to do that. – H. Doebler Aug 18 '21 at 11:14
  • it doesn't matter if you create module or not. if `main.py` and `videotoimage.py` are in the same folder then you should use `from videotoimage`. Method with `sys.path` will be useful when you will have to run code from different folder (different `Current Working Director` - `os.getcwd()`) - like. `python c:\preprocessing\main.py` because then it will search `videotoimage` in different folder and you will need something like `os.path.dirname(os.path.abspath(__file__))` to get folder with code - and add it to `sys.path` before importing – furas Aug 18 '21 at 11:41

2 Answers2

0

use:

from videotoimage import *
ilpianoforte
  • 1,180
  • 1
  • 10
  • 28
0

Preprocessing is not a package in your tree. Either you can make it a package by inserting an __init__.py file, or you can just call it relatively:

from videotoimage import *
  • 1
    Or even better: make the relative import explicit by using `from .videotoimage import *`. If `from <...> import *` is considered 'good' at all, but thats a different question. – H. Doebler Aug 18 '21 at 11:05
  • Good suggestion, added it to the answer. – Michiel Aarts Aug 18 '21 at 11:07
  • I tried the previous solution, `from videotoimage import *` and it worked, however, your updated answer doesn't work, it gives an `ImportError: attempted relative import with no known parent package` –  Aug 18 '21 at 11:13
  • Oh, my mistake. Forgot that you did not already had a `__init__.py`. Anyway, you should go that way, because it avoids name clashes if you happen to install a package called `videotoimage` in the future. – H. Doebler Aug 18 '21 at 11:20
  • True, but for the sake of simplicity of this question, the import without the dot will work. Also see the answer to this question: https://stackoverflow.com/a/63312776/16689404 – Michiel Aarts Aug 18 '21 at 11:28