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
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
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.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1364
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: metulburr and 6 guests