0

I am trying to access the Amazon Advertising through Python and I created a Python script to automate the authentication process. This file, called amazon.py is located in ~/PROJECT/APP/amazon.py.

I want to be able to play around with the API, so I launched python manage.py shell from the ~/PROJECT directory to enter the Python shell. My goal is to be able to execute the python script amazon.py within this shell. What command should I be using to execute amazon.py?

2 Answers 2

1

Normally, you just import the file and call a function within it:

import APP.amazon
APP.amazon.main() 

This would only work if amazon.py is laid out like this:

def main():
    ...code...

if __name__ == '__main__':
    main()

Also, in the directory ~/PROJECT/APP there needs to exist a file __init__.py with nothing in it, or else Python won't see APP as a package with the module amazon in it.

Disclaimer: I don't actually know what manage.py does.

Sign up to request clarification or add additional context in comments.

Comments

1

Normally a script is "executed" upon import. I'd suggest you wrap your functionality in amazon.py in a function:

def call_functionality():
    ...

In your shell you can now import it with:

import path.to.amazon as amazon

and then execute it by

amazon.call_functionality()

3 Comments

Thanks, this helped! I ended up creating an authenticate() function in amazon.py. In authenticate(), I declared an amazon_api object that serves to access the API. How can I make this available to me after I execute amazon.authenticate() from the shell? I'm guessing the reason it doesn't exist is because it's a local variable?
Nevermind, answered my own question. I can just return the object.
Exactly. Otherwise if you save this in a module object you could still access it by amazon.somevariable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.