Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:47:47+00:00 2026-06-05T12:47:47+00:00

How do I execute all the code inside a python file so I can

  • 0

How do I execute all the code inside a python file so I can use def’s in my current code? I have about 100 scripts that were all written like the script below.

For a simple example, I have a python file called:

D:/bt_test.py

His code looks like this:

def bt_test():
    test = 2;
    test += addFive(test)
    return(test)

def addFive(test):
    return(test+5)

Now, I want to from a completely new file, run bt_test()

I’ve tried doing this:

def openPyFile(script):
    execfile(script)

openPyFile('D:/bt_test.py')
bt_test()

But this doesn’t work.

I’ve tried doing this as well:

sys.path.append('D:/')
def openPyFile(script):
    name = script.split('/')[-1].split('.')[0]
    command = 'from  ' + name +  ' import *'
    exec command

openPyFile('D:/bt_test.py')
bt_test()

Does anyone know why this isn’t working?

Here’s a link to a quicktime video that will help explain what’s happening.
https://dl.dropbox.com/u/1612489/pythonHelp.mp4

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:47:49+00:00Added an answer on June 5, 2026 at 12:47 pm

    In addition to Ned’s answer, __import__() might be useful if you don’t want the file names hardcoded.

    http://docs.python.org/library/functions.html#__import__

    Update based on the video.

    I don’t have access to Maya, but i can try and speculate.

    cmds.button(l='print', c='bt_press()') is where the issue seems to lurk. bt_press() is passed as a string object, and whatever way the interpreter uses to resolve that identifier doesn’t look in the right namespace.

    1) Try passing bt_press() with the module prepended: cmds.button(l='print', c='bt_test.bt_press()')

    2) See if you can bind c directly to the function object: cmds.button(l='print', c=bt_press)

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script that will execute three applications. All of it have
I can successfully use Python to create a database and run the execute() method
I found on website all about the answer to that question, the code changes
SBT has a nice hook which allows you to execute arbitrary code after all
First, an example of something that works as expected: (all code was executed in
I've been trying to write a function that makes all links execute a function
To execute my application in iPhone first of all I have to add provisioning
I put this below code inside my *.dll for Internet Explorer. FILE *child =
How do I execute this code inside a Thread object? I want continuous execution,
I have a method that is going to the DB so all our JDBC

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.