run .py files with .py files

This is the place for queries that don't fit in any of the other categories.

run .py files with .py files

Postby mckryall » Tue Jan 14, 2014 2:23 am

How, in code, can I run a separate .py file without importing it's functions? Do I absolutely have to make it run a console command?
Last edited by Mekire on Tue Jan 14, 2014 6:15 am, edited 1 time in total.
Reason: Lock.
Python 2.7
Mac Mini (Desktop/server)
Macbook Air
Laptop w/ Ubuntu 14.04
mckryall
 
Posts: 116
Joined: Sat Nov 23, 2013 6:13 pm

Re: run .py files with .py files

Postby metulburr » Tue Jan 14, 2014 3:23 am

python3.x
Code: Select all
exec(open('filename.py').read())


python2.x
Code: Select all
execfile('filename.py')


Or like you said, you can have the OS call it via subprocess module.
However if the program is integrated, it would be better to import it.
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2231
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 8 guests