-2

I'm trying to import a function from another python script that is located in the same folder as my current script but it throws ModuleNotFound error.

I checked the path in which Pycharm looks for the module (in this case the spambot.py) and it does look through the current folder for the module so i don't understand why the module isn't found.

Problem Path

alex
  • 10,900
  • 15
  • 70
  • 100
Jackson
  • 17
  • 4
  • 2
    add `__init__.py` file in the same directory. Then you can import specific files. – Prakhar Sharma Aug 16 '22 at 08:30
  • 1
    `import os`, `print(os.getcwd())` before importing the module, and post the results. https://stackoverflow.com/a/73363599/4788546. – CristiFati Aug 16 '22 at 08:36
  • Also, does it happen with pure Python? Is it something to do with Pycharm? – Nishant Aug 16 '22 at 08:38
  • @PrakharSharma i added the init python file in the same folder as both the script that contains the function, and also the script that i want to import that function but still the same ModuleNotFoundError: No module named 'spambot'. did i create the _init__.py file in the wrong place? – Jackson Aug 18 '22 at 07:03
  • Ok, have a look at my library [here](https://github.com/praksharma/DeepINN/tree/main/DeepINN/utils). Check how I wrote my `__init__.py`. Just see the import definition, ignore everything after this line `def _load_backend(mod_name):` – Prakhar Sharma Aug 18 '22 at 08:56
  • created the file in the same folder as my other scripts. in the init file this is what i tried: trial 1: from .spambot import * trial 2: from .spambot import launch doesnt work for both with the same problem 'unresolved reference: spambot' but when i tried my solution, posted in the answer section now, it worked. Thanks for your help – Jackson Aug 20 '22 at 14:17

2 Answers2

0

This works for me, when I get your problem.

from .spambot import launch_chrome

Nishant
  • 20,354
  • 18
  • 69
  • 101
-1

recap: i wanted to import a function (named launch) from another script in the same directory (whatsapp folder)

how i solve the problem (though idt its the best solution)

right-click the current directory (whatsapp folder in my case) and create a python package (i name it funcs) in the directory. in the package, create a init.py file. In the init file, put in the functions that you will import on daily basis.

with the above set up, all u need to do is to go to the script u want to import the function and type this: from (python package) import (function name) in my case it would be: from funcs import launch enter image description here

Jackson
  • 17
  • 4