0

I want to import some files/modules from other directory. I have placed an init.py in the directory but still not able to import the files/modules. I am using Pycharm 2019.

I want to use init.py only method and not the sys.path.append.

thanks in Advance!

  • I just realized from your other comments that you have `init.py` files instead of `__init__.py` files, which is probably why you can't import anything from these folders (assuming I understood you correctly). Change the filenames to `__init__.py` and try it – Ofer Sadan Oct 06 '19 at 09:56
  • thanks ofer Sadan, but I am typing ____init____.py and it autocorrecting to init.py in the comment. – Martyn Mystery Oct 09 '19 at 06:28

1 Answers1

0

there are multiple ways to do that, I'll name two -

import sys
sys.path.insert(0, 'path/to/your/py_file')

import py_file

is relatively easy, and another one is if you're using pycharm just mark the directory of which you want to import from as a sources root.

Yoel Nisanov
  • 984
  • 7
  • 16
  • actually I am also using the pycharm's default python package making system. but it is still giving me nothing as I am unable to import the module to the current directory. so please give me solution as I am new to the programming. – Martyn Mystery Oct 06 '19 at 06:47
  • thanks Yoel Nisanov but as I said i dont want to use the sys.path method but __init__.py – Martyn Mystery Oct 06 '19 at 06:50
  • all my projects are in PycharmProject dir, and all the projects are including the __init__.py file. but still I am not able to import any file to the different dir, but I am able to import in the same dir. – Martyn Mystery Oct 06 '19 at 06:55
  • @OferSadan Hi mate, as you probably saw I gave him 2 options BECAUSE he said he doesn't want to use sys.path.append. And since he said he is using pycharm I think the solution I provided should be enough. – Yoel Nisanov Oct 06 '19 at 09:54
  • listen @MartynMystery, mark the directory you want to import from as sources root this should solve your issue... If it still doesn't work please notify me... – Yoel Nisanov Oct 06 '19 at 09:55
  • @YoelNisanov I'm retracting my comment beacause perhaps you're right and I judged too quickly – Ofer Sadan Oct 06 '19 at 09:57
  • @OferSadan it's 100% fine (: – Yoel Nisanov Oct 06 '19 at 10:03