0

I am trying to import files (python files) from another directory but it is not working as I have tried.

/pythonproject
.main.py
.__init__.py
 ->folder1
   ->.file1.py
   ->.file2.py
   ->.__init__.py
 ->folder2
   ->.functions.py
   ->.globals.py
   ->.__init__.py

I am trying to import functions.py inside of my file2.py.

I have tried

from functions import *
import functions

#file2.py
sys.path.insert(0, '/pythonproject')
import functions
Doritos
  • 403
  • 3
  • 16
  • Possible duplicate of [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – SPYBUG96 Nov 05 '18 at 20:15
  • When you run just the code you have provided to us, what errors are you getting? Remember when you are importing and want to use one of the functions in the other file you need to use `functions.NameOfFunctionHere()` – SPYBUG96 Nov 05 '18 at 20:21
  • That was the issue. I imported only the function I needed and it worked. I also switched it to import * and it worked. Thank you. – Doritos Nov 05 '18 at 20:22
  • I had a sneaking suspicion that was the case – SPYBUG96 Nov 05 '18 at 20:27

2 Answers2

1

I think your friend for this case is sys.path.append.

Zoe
  • 27,060
  • 21
  • 118
  • 148
camp0
  • 372
  • 1
  • 5
0

As discovered the problem is that you are importing the file, but when you went to use a function in the file you couldn't

One solution is:

import functions

functions.NameOfFunctionHere()
SPYBUG96
  • 1,089
  • 5
  • 20
  • 38