0

I created a new project (myproject) and 3 python files in a row (mainscript.py, module1.py, module2.py) in the directory. In main script, I coded:

import module1
import module2

The two empty modules were imported without problem. After this, I created a 3rd python file (module3.py) in the same directory. When I tried to run:

import module3

I got the message:

Traceback (most recent call last):
  File "E:\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-6-b4e6aa7db43e>", line 1, in <module>
    import module3
  File "E:\pycharm\PyCharm Community Edition 2020.1.2\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'module3'

Can someone explain why the first two files are detected and not the third?

After trying several times the experiment, it seems that only the files created before I run for the first time import function will be found. screenshot

rbtlm640
  • 79
  • 5
  • Are you getting that error when you use python console? – isAif Jun 23 '20 at 17:08
  • I am also getting an error from the python console, but that is because the python programme is in another file and I do not know how to specify the directory. Regarding the import on Charm, I realised that when I close and open the program again the other created modules can be imported. However this means I have to close and open Pycharm very time I create or change one of my modules. – rbtlm640 Jun 25 '20 at 06:51
  • Try the following in terminal and show what output you are getting: `python --version` – isAif Jun 25 '20 at 07:05
  • I obtain this whith `python --version` : `Python 3.7.6` – rbtlm640 Jun 25 '20 at 07:48
  • Create an empty python file with the name `__init__.py` in the directory. – isAif Jun 25 '20 at 07:55
  • I added one in my directorybut it is still not working – rbtlm640 Jun 25 '20 at 08:00
  • `import sys sys.path.append('my/path/to/module/folder')` and use your complete directory path. – isAif Jun 25 '20 at 08:04
  • sadly that didn't solve the problem – rbtlm640 Jun 25 '20 at 08:09
  • Check this link and see if solves your issue: https://stackoverflow.com/questions/15514593/importerror-no-module-named-when-trying-to-run-python-script – isAif Jun 25 '20 at 08:10

0 Answers0