0

I can not import my module containing python class. Everywhere it's said that if both files are in the same directory import should work fine but it seems like it doesn't. I have the following structure of folders which ar ein the same folder:

chapter_9 folder contains files:
9-10.py
9-1.py
9-4.py
9-7.py
9-9.py
restaurant.py

when I type the following in the 9-10.py file

import restaurant

it's saying "No module named restaurant." Why it is so? Both files are in the same folder. Tell me please if you know where I should dig?

Thanks.

Azat
  • 83
  • Have you tried making an empty file called __init__.py? https://stackoverflow.com/questions/4142151/how-to-import-the-class-within-the-same-directory-or-sub-directory – Katu Apr 19 '18 at 10:13
  • I have tried to create empty init.py. No result. Also I tried to call like this: from .restaurant import Restaurant and then I get the error: No module named 'main.restaurant'; 'main' is not a package – Azat Apr 19 '18 at 10:59
  • 1
    It' said in the link you have posted previously https://askubuntu.com/questions/470982/how-to-add-a-python-module-to-syspath that if it's module and it's in the same folder as the calling app or script I can simply write import restaurant and this should work but it doesn't. So seems confusing to me – Azat Apr 19 '18 at 11:06
  • also making initi.py is required only if it's package but I have simple module. So no need for me doing that as I get this. – Azat Apr 19 '18 at 11:13
  • @Azat In pycharm, did you open the chapter_9 folder or only the separated python files ? – yoann_dev Apr 20 '18 at 09:10
  • @yoann_dev, hmmm... I have created chapter_9 folder and then inside I have created python files and then opened them. Also I even incidentally have added to my sys.path variable the path to the chapter_9 folder and this also didn't help.(import sys sys.path.insert(0, "/path/to/your/package_or_module") Also there is ' ' in my sys.path variable which means it should search for the modules in the current location. – Azat Apr 20 '18 at 13:47
  • import restaurant works perfectly in the shell but doesn't work in pycharm. it's something to do with pycharm but I don't know what exactly – Azat Apr 20 '18 at 17:55
  • @Azat I added a new part in my answer to Open projet in Pycharm – yoann_dev Apr 23 '18 at 12:53

1 Answers1

1

Open project in PyCharm

You need to open the whole chapter9 folder in pycharm. In Pycharm do : File > Open and choose chapter9 folder.

enter image description here

Projet folder appears in left side of Pycharm

enter image description here

To finish you can check python interpreter and root directory. Go in File > Settings > Project: Chapter9.

  • Project Interpreter must be valid.
  • Project Structure must have a valid content root (chapter9 folder)

enter image description here

PYTHONPATH

To ensure that Python search modules in your directory, you can set PYTHONPATH environnement variable by the following command :

export PYTHONPATH=$PYTHONPATH:/path_to/chapter_9 folder/

Documentation to PYTHONPATH : https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH

yoann_dev
  • 151
  • when I type export PYTHONPATH=$PYTHONPATH:/path_to/chapter_9 folder/ pycharm is underlining it with red saying that it's unresolved reference. – Azat Apr 19 '18 at 11:15