0

My current project has the following structure:

src:
   file1.py
   file2.py

There are some functions in file2 which I would like to import into file1 and use. So the file 1 has the following lines:

from file2 import func1, func2

When run the terminal in the src directory and type:

from file1 import *

everything works well. However, when go outside the directory src, and type in the python terminal

from src.file1 import *

I get the following error:

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/bhavincvts/Desktop/greenleap/solarAFD1/src/file1.py", line 3, in <module>
    from file2 import func1, func2 
ModuleNotFoundError: No module named 'file2'

After that, I tried changing the import statement to,

from .file2 import func1, func2

it then works well from outside the src folder. But when running the terminal inside the src folder, it shows the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/bhavincvts/Desktop/greenleap/solarAFD1/src/file1.py", line 3, in <module>
    from .file2 import func1, func2 
ImportError: attempted relative import with no known parent package

Is there anyway to fix this?

  • 2
    you are up for a long day understanding python imports, longstory short file1 ccan not see file2, (when running inside src your pythonpath can see it), put an \__init__.py file inside src and try again, with the from .file2 import .... – E.Serra Jun 08 '20 at 14:42
  • 1
    I would try what @E.Serra said... everytime I think I understand python imports I end up in a situation that boggles me..another option would be to just add src to your path – Derek Eden Jun 08 '20 at 14:43
  • 1
    In short, you have to decide whether ``src`` is a package or not. That is, whether ``file2.py`` should be known as ``src.file2`` or just ``file2``. Doing both at the same time is not a good idea. – MisterMiyagi Jun 08 '20 at 14:55
  • Does this answer your question? [Import a module from both within same package and from outside the package in Python 3](https://stackoverflow.com/questions/47319423/import-a-module-from-both-within-same-package-and-from-outside-the-package-in-py/50392363#50392363) – MisterMiyagi Jun 08 '20 at 15:02
  • oh yes, I sort of understood that while I was writing the question. I was just wondering if there was some way you could use the file either way. – Varun Desai Jun 08 '20 at 15:08
  • @MisterMiyagi it sort of does, thanks! – Varun Desai Jun 08 '20 at 15:12

2 Answers2

-1

got it;

file1.py:

from file2 import func1, func2
func1()
func2()

file2.py has the functions:

def func1():
    print('yeeeeah func1')

def func2():
    print('yeeeah func2')

src: __init__.py file1.py file2.py

from src:

src$ python file1.py 

Prints:

yeeeeah func1
yeeeah func2

from somewhere else:

src$ cd ..
test_stack$ python src/file1.py 

Prints:

yeeeeah func1
yeeeah func2

Assuming src is your main directory (where setup.py requirements.txt etc live)

E.Serra
  • 1,495
  • 11
  • 14
  • Yes, this works well for me too. However, if you try running a python terminal somewhere else (lets say in the directory containing the src folder), it'll throw you an error... – Varun Desai Jun 08 '20 at 15:06
  • no, this works from src and from outside as long as you have the init.py file – E.Serra Jun 08 '20 at 15:06
  • @E.Serra Does your ``python`` point to Python *2*? Python3 removed implicit relative imports. – MisterMiyagi Jun 08 '20 at 15:07
  • i am using python3, the __init__.py file indicates the directory can be usedd as a module – E.Serra Jun 08 '20 at 15:09
-2

In file1, before you import the functions from file2, you can specify the directory of file2:

import sys
sys.path.append("/path_to_the_directory_that_can_find_file2")

from file2 import func1, func2

This should make it more robust against running outside the src directory.

hitc
  • 177
  • 1
  • 5
  • dont do this, it is bad practice, learn how to use proper imports – E.Serra Jun 08 '20 at 14:55
  • This might duplicate any module inside ``src``, which can lead to various non-obvious bugs (e.g. ``isinstance`` failing between the two versions). – MisterMiyagi Jun 08 '20 at 14:57