0

I want to make a file that is consisted of 3 python programs. but, when I want to access one of the there files from one of them, it cant find the folder.

I made a init python file in it so python can recognize it as a module

my folder struct:

dlgo/
        __init__.py
        goboard_slow.py
        gotypes.py

my goboard_slow:

from dlgo.gotypes import player

error:

Traceback (most recent call last):
  File "C:\Users\asus\Desktop\dlgo\goboard_slow.py", line 2, in <module>
    from dlgo.gotypes import player
ImportError: No module named 'dlgo'
  • You're already inside that module; see e.g. https://docs.python.org/3/tutorial/modules.html – jonrsharpe Sep 15 '19 at 12:17
  • You can take Misieq's answer - however, make sure your $PYTHONPATH is set up correctly (e.g. if you're working with Pycharm, it may be set to the root). To check your $PYTHONPATH you can `import sys; print(sys.path)` – GregK Sep 15 '19 at 12:35

4 Answers4

0

maybe try from (filename) import (functionname)

Virtual Dreamer
  • 43
  • 1
  • 12
0

Access as below:

from dlgo.gotypes import players

See here more info on "Guido's decision" on imports in python 3 and complete example on how to import in python 3.

Kaushal Pahwani
  • 464
  • 3
  • 11
0

Tl;dr:

from gotypes.py import player

when you specify path to a file, interpreter starts looking for it inside same folder, unless you give it path from main dirrectory like '/' or 'C:\'

Misieq
  • 507
  • 2
  • 12
0

You can import a py file with the following statement:

# Other import
import os
import sys

if './dlgo' not in sys.path:
    sys.path.insert(0, './dlgo')
from dlgo.gotypes import player

NOTE:
For IDE like PyCharm, you can specify the import path using the Project Structure setting tab (CTRL+ALT+S)

Helpful stack overflow questions [maybe off topic]:
What is the right way to create project structure in pycharm?

Manage import with PyCharm documentation:
https://www.jetbrains.com/help/pycharm/configuring-project-structure.html

alessiosavi
  • 2,753
  • 2
  • 19
  • 38