Python Forum
Python call/running another python file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python call/running another python file
#1
Hello World!

I have two files python, ex: file1.py and file2.py

Now I'm working in file2.py and I need to run all the python code from file1.py inside(when I'm running) the file1.py.

how I can make this possible?

I already saw some websites that i have to do:

1
import file1.py
in the beginning, but I don't know how to run.

And last question, I will have problems with same variables used in the files, or doesn't matter
Reply
#2
1
import file1
Then you can use the classes and functions that are defined in file1
Reply
#3
Example

file1.py
1
2
3
4
variable = 'variable'
 
def file1_function(text):
    print(f'output from file1: {text}')
file2.py
1
2
3
4
import file1
 
print(f'output from file2: {file1.variable}')
file1.file1_function('from file2')
output from running file2.py
Output:
output from file2: variable output from file1: from file2
Reply
#4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
 
 
 
def init_driver():
    driver=webdriver.Firefox(executable_path="C:\\Users\\jpedr\\Desktop\\geckodriver-v0.26.0-win64\\geckodriver.exe")
    driver.wait=WebDriverWait(driver, 5)
    driver.get("www.")
    return driver
 
 
def get_data(b,driver):
    try:
        driver.refresh()
        import time
        time.sleep(5)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        d1=driver.find_element_by_xpath("/html/body/div[2]/main/div/div/div[2]/div/div/div/div[3]/div[2]/section[1]")
        b=b+1
        print("casa",b)
         
    except NoSuchElementException as e:
        print("no more casa total=",b)
        get_data.b=b
     
 
 
 
 
 
 
driver=init_driver()
 
 
import time
time.sleep(5)
driver.refresh()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
import time
time.sleep(2)
i=0
try:
    e1=driver.find_element_by_xpath("/html/body/div[2]/main/div/div/div[2]/div/div/div/div[3]/div[2]/section[1]")
    i=i+1
    print("casa",i)
    e2=driver.find_element_by_xpath("/html/body/div[2]/main/div/div/div[2]/div/div/div/div[3]/div[2]/section[2]")
    i=i+1
    print("casa",i)
     
except NoSuchElementException as e:
    print("numero de casas Ă©", i)
 
 
get_data(0,driver)
 
print (get_data.b)
 
import principal
quit()
while True:
    try:
        if i==get_data.b or get_data.b<i:
            import time
            time.sleep(60)
            print("step 1")
            get_data(0,driver)
            print(get_data.b)
            print("repetition")
    except ValueError:
        print("changed")
 
if i!=get_data.b and get_data.b>i:
    print("runother py file")
    import principal
    print("finished")
    pass
I having a error

Error:
Traceback (most recent call last): File "C:\Users\jpedr\Documents\tes3.py", line 109, in <module> import principal ModuleNotFoundError: No module named 'principal'
in this case is line 62, I just remove some lines for be short
Reply
#5
windows11

you code is not very clear from what you have pasted - i can not determine if that is all one .py file or you have multiples there and what modules hold what code. Python requires you to define any functions etc before you can reference and same with variables, it also by good practise would expect all your import / from statements to be at the top of the module, thus letting any reader of your code to understand what packages/modules are required and then being to apply to the following code.
If you re-post your code (assuming it is not all one module) it may be easier to acertain where you error is arising.

also be sure to post your code inside python tags to aid clarity
Regards
-------- *
“Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.”
Reply
#6
This is the two files that I have.
I need to run the principal file inside from test.

the principal file works perfectly.

And I cal the principal in the line 78 from test file.

the error appear is
import principal
ModuleNotFoundError: No module named 'principal'

Hope now is clear my situation

file name: test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
 
 
 
def init_driver():
    driver=webdriver.Firefox(executable_path="C:\\Users\\jpedr\\Desktop\\geckodriver-v0.26.0-win64\\geckodriver.exe")
    driver.wait=WebDriverWait(driver, 5)
    driver.get("https://www.")
    return driver
 
 
def get_data(b,driver):
    try:
        driver.refresh()
        import time
        time.sleep(5)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        d1=driver.find_element_by_xpath("/html/body/div[2]/section[1]")
        b=b+1
        d2=driver.find_element_by_xpath("/html/body/div[2]/section[2]")
        b=b+1
        #etc....
    except NoSuchElementException as e:
        print("no more, total=",b)
    get_data.b=b
     
 
 
 
 
 
 
driver=init_driver()
 
 
import time
time.sleep(5)
driver.refresh()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
import time
time.sleep(20)
i=0
try:
    e1=driver.find_element_by_xpath("/html/body/div[2]/section[1]")
    i=i+1
    e2=driver.find_element_by_xpath("/html/body/div[2]/section[2]")
    i=i+1
    #etc...
except NoSuchElementException as e:
    print("total", i)
 
 
get_data(0,driver)
print ("i=",i)
print ("get_data.b=",get_data.b)
 
while True:
    print ("get_data.b=",get_data.b)
    if i==get_data.b or get_data.b<i :
        try:
            import time
            time.sleep(5)
            print("replay funtion")
            get_data(0,driver)
            print("b", get_data.b)
        except ValueError:
            print("something happen")
            break
    if i!=get_data.b and get_data.b>i:
        break
    pass
 
 
if i!=get_data.b and get_data.b>i:
    print("run another python file")
    import principal
    print("principal finished")
    pass
file name: principal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
 
 
def init_driver():
    driver=webdriver.Firefox(executable_path="C:\\Users\\jpedr\\Desktop\\geckodriver-v0.26.0-win64\\geckodriver.exe")
    driver.implicitly_wait(5) # seconds
    driver.wait=WebDriverWait(driver, 15)
    driver.get("https://www.")
     
    return driver
 
 
def my_function(i,driver):
    while i<b:
        import time
        time.sleep(15)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        element=driver.wait.until(EC.presence_of_element_located((By.XPATH,f[i])))
         
        while True :
            try:
                if find_element_by_xpath(f[i]) is True:
                    element1=driver.find_element_by_xpath(f[i])
                    driver.find_element_by_xpath(f[i]).click()
            except ValueError:
                print("no more")
 
            pass
         
        try:
            element = driver.find_element_by_xpath(f[i])
            driver.execute_script("arguments[0].click();", element)
        except NoSuchElementException as e:
            print("no")
        try:
          
            myDynamicElement = driver.find_element_by_xpath("/html/body/div[2]/div/form/input")                                                 
            driver.find_element_by_xpath("/html/body/div[2]/main/input").click()          
         
        except NoSuchElementException as e:
            driver.wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/div/form/input")))
        import time
        time.sleep(2)
        driver.back()
        i=i+1
 
a=0
i=0
b=0
bypass=0
 
 
 
 
 
driver=init_driver()
 
 
     
f1="/html/body/section[1]"
f2="/html/body/section[2]"
 
 
 
 
f=[f1,f2]
 
driver.wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/section[1]")))
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
try:
 
    c1=driver.find_element_by_xpath("/html/body/section[1]")
    a=a+1
    
except NoSuchElementException as e:
    print("no")
     
import time
time.sleep(10)
if a>0:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    driver.find_element_by_xpath("/html/body/div[3]").click()
    element=driver.wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/section[1]")))
    driver.find_element_by_xpath("/html/body").click()
    try:
         
        myDynamicElement = driver.find_element_by_xpath("/html/body/div[2]")                                                 
     
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        import time
        time.sleep(2)
 
        driver.find_element_by_xpath("/html/body").click()          
     
        try:
            myDynamicElement = driver.find_element_by_xpath("/html/body/div[2]
            driver.find_element_by_xpath("/html/body/div[2]").click()
            driver.wait.until(EC.presence_of_element_located((By.NAME,"username")))
            driver.find_element_by_name("username")
            import time
            time.sleep(2)
            driver.find_element_by_name("username").send_keys("")
            driver.wait.until(EC.presence_of_element_located((By.NAME,"password")))
            driver.find_element_by_name("password")
            import time
            time.sleep(2)
            driver.find_element_by_name("password").send_keys("")
        import time
        time.sleep(2)
        driver.wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/div[2]")))
        driver.find_element_by_xpath("/html/body")
        driver.find_element_by_xpath("/html/body").click()          
         
     
        import time
        time.sleep(2)
 
 
 
 
try:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    d1=driver.find_element_by_xpath("/html/body/div[2]/main/")
    b=b+1
    d2=driver.find_element_by_xpath("/html/body/div[2]")
    b=b+1
     
except NoSuchElementException as e:
    print("no more")
 
 
 
 
 
if b>0:
    my_function(0,driver)
 
   
Reply
#7
What are the absolute paths to your python files? When you 'import principal' it must be in the same directory as your 'test.py' file.

Also, in your principal file line 102 is missing double quotation mark and a parenthesis - is that in your real file?
Another thing, you use 'time.sleep()' so many times, and every time you use it, you 'import time'. Why don't you just put 'import time' at the top of your scripts so you don't need to keep importing it?
Reply
#8
Thank you, the problem was the files was in different folders.
And the principal I remove parts from X_path because was to big and maybe was more difficult to read, and I delete that, by mistake in the line you say. But the principal file runs well.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python code not running Azdaghost 1 278 Apr-22-2025, 08:44 PM
Last Post: deanhystad
  How to write variable in a python file then import it in another python file? tatahuft 4 1,001 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Python: How to import data from txt, instead of running the data from the code? Melcu54 1 669 Dec-13-2024, 06:50 AM
Last Post: Gribouillis
  No Internet connection when running a Python script basil_555 8 3,422 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 7,224 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Is possible to run the python command to call python script on linux? cuten222 6 2,454 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  How to check if Skype call is connected or disconnected in Python? boral 1 1,193 Dec-28-2023, 08:31 AM
Last Post: buran
  Help Running Python Script in Mac OS emojistickers 0 976 Nov-20-2023, 01:58 PM
Last Post: emojistickers
  error "cannot identify image file" part way through running hatflyer 0 2,042 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Python Launcher Pops Up When Py-based App Is Running (Mac) radix_optimus 0 1,184 Sep-18-2023, 09:22 AM
Last Post: radix_optimus

Forum Jump:

User Panel Messages

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