Python Forum
Calling functions from another file
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling functions from another file
#11
You can see what your program will, and will not see, by running this simple script
(from interactive python)
>>> import sys
>>> sys.path

If you don't see the directory where your second program is located, it wont be found.
__init__.py will fill in the gaps.
Reply
#12
my_unitest folder in sys.path,structured as packages.
Use pytest  Cool
my_uintest/
  folder_a/
  |-- __init__.py
  |-- file_a.py
  folder_b/
  |-- __init__.py
  |-- file_b.py
file_a.py:
import unittest

class A(unittest.TestCase):
   def testA(self):
       self.assertEqual(42, 43)
file_b.py:
import unittest
from my_uintest.folder_a import file_a
  
class B(unittest.TestCase):
   def testA(self):
       file_a.A.testA(self)       

if __name__ == '__main__':
    unittest.main()  
Test:
Output:
λ python file_b.py F ====================================================================== FAIL: testA (__main__.B) ---------------------------------------------------------------------- Traceback (most recent call last):   File "file_b.py", line 6, in testA     file_a.A.testA(self)   File "C:\Python36\my_uintest\folder_a\file_a.py", line 5, in testA     self.assertEqual(42, 43) AssertionError: 42 != 43 ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 749 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  How can i combine these two functions so i only open the file once? cubangt 4 805 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Calling a function (which accesses a library) from another file mouse9095 4 767 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  Functions to consider for file renaming and moving around directories cubangt 2 1,701 Jan-07-2022, 02:16 PM
Last Post: cubangt
  Calling functions from within a class: PYQT6 Anon_Brown 4 3,645 Dec-09-2021, 12:40 PM
Last Post: deanhystad
Question trouble with functions "def", calling/defining them Duck_Boom 13 4,235 Oct-21-2020, 03:50 AM
Last Post: Duck_Boom
  Calling C functions with PyObjects jibarra 6 2,657 Jul-17-2019, 02:52 PM
Last Post: jibarra
  Duplicate output when calling a custom function from the same file? road2knowledge 2 2,344 May-10-2019, 07:58 AM
Last Post: road2knowledge
  Use Variables Generated from Functions in different files to use on the main file AykutRobotics 3 2,878 Jan-01-2019, 04:19 PM
Last Post: AykutRobotics
  calling os functions not in module os Skaperen 2 2,589 Nov-10-2018, 01:54 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020