0

When I write a program in Python, I put everything in a script. Sometimes I need to experiment with modules, classes and methods before using them in my program and use the interpreter for this. At some point, I need to test stuff in the context of my program so I need the classes, methods and variables from my program.

Example: I want to experiment with sending XHR requests but first I need to do things my program do (instantiate classes, set some attributes, craft the url, parse the page,...). My workflow so far:

  • Sometimes I just Copy/Paste the relevant parts from the script to the program which is tedious
  • If I use my script as a module, I can do:

    import myprogram
    a = myprogram.test()
    ...
    

I heard about exec() which is very tedious to use (the whole command). When I used Matlab long time ago, I could simply execute a script in the interpretor then I would tinker with variables until I had the result I wanted.

How do you source a whole script in the interpreter?

funkygoby
  • 13
  • 6
  • You can copy/paste it. But my advice would be to import it into the interpreter (assuming you are working outside the python path you'll need to import by explicit path: [see this](https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path)). Also it would really save you a lot of work if you just started using [doctest](https://docs.python.org/3.6/library/doctest.html) in your documentation. – armatita Oct 27 '17 at 14:34
  • Thank you. Importing is fine. It seems the best option so far. I will have a look at doctest but it doesn't seem to really fit my workflow. I just need to quickly tests new stuff before adding them in my script. – funkygoby Oct 30 '17 at 10:30

0 Answers0