Using a virtualenv in Drawbot



  • I often set up a different virtual environment for different Python-based scripting ideas that I run in the terminal. I will do this using virtualenv and pip install the needed modules.

    So my question is can I select a Python virtualenv from within the Drawbot app or a script that I'm running in Drawbot?

    I have tried adding a shebang at the top of the script pointing to the virtualenv's python. I have also tried something like this:

    activate_this_file = "/Users/maartenidema/Desktop/t_venv/bin/activate_this.py"
    execfile(activate_this_file, dict(__file__=activate_this_file))
    

    I feel like I am really overlooking something quite simple.



  • @maarten-idema I'm fairly sure you're the first to try such a thing... It may be difficult or impossible, because by the time DrawBot is running, Python is running, and you can't really change the environment much anymore. That said, I know very little about how virtual environments work, so take this with a grain of salt.



  • Thanks for that @justvanrossum. I am using the Python modules todoist, requests, and urllib3 to visualise my todo list in Drawbot.

    After reading this I kind of resolved to manually downloading each module and have them sit in the same folder as my Drawbot script. This is so tedious especially for updating modules.

    But then I realised that I could still set up my virtualenv, pip install my modules, their dependencies and then in my Drawbot script include the path to the site-packages.

    import sys
    sys.path.insert(0, "/Users/maartenidema/t_venv/lib/python3.6/site-packages")
    

    On a quick initial test, this seems to work. I wonder if there will be module conflicts with Python versions going forward.


  • admin

    This will not work from within the app itself.

    But you can always install drawBot as module and go from there, you can even pop up a drawBot window within the controlled env as drawBot is just plain vanilla.



  • It does actually work fine from within DrawBot itself. I have been using this since the end of Jan and haven't had an issue.



  • @frederik You mentioned opening a drawBot window when used as a module.
    How to do that?


  • admin

    this should show a small preview with the same views as what drawBot is using

    import vanilla
    import drawBot
    from drawBot.ui.drawView import DrawView
    
    
    class PreviewController(object):
        
        def __init__(self):
            self.w = vanilla.Window((400, 400))
            self.w.view = DrawView((0, 0, 0, 0))
            
            self.drawSomeThing()
            
            self.w.open()
    
        def drawSomeThing(self):
            # draw something
            drawBot.newDrawing()
            drawBot.newPage(300, 300)
            drawBot.fill(1, 0, 0)
            drawBot.rect(20, 20, 200, 200)
            # get the pdf document
            pdfDocument = drawBot.pdfImage()
            drawBot.endDrawing()        
            # set the pdf document in the view
            self.w.view.setPDFDocument(pdfDocument)
    
    
    from vanilla.test.testTools import executeVanillaTest
    executeVanillaTest(PreviewController)
    


  • @frederik thanks! the technique is now clear to me;
    but where do i get the vanilla.test module from?

    from vanilla.test.testTools import executeVanillaTest
    ModuleNotFoundError: No module named 'vanilla.test'
    

  • admin

    you need to have vanilla installed to build an ui in a pythonic way.

    see https://github.com/typesupply/vanilla/tree/master/Lib/vanilla



  • @frederik of course i do. but my installation didn't have the vanilla/test folder apparently. perhaps the setup doesn't include this?

    anyways, i manually copied the files to an appropriate location and now it's working.



  • Oh, that's a clever idea, thanks for sharing.

    ... said in Using a virtualenv in Drawbot:

    import sys
    sys.path.insert(0, "/Users/maartenidema/t_venv/lib/python3.6/site-packages")

    You should be able to make that happen automatically. Please try the following:

    • Create a folder called /Library/Python/3.6/site-packages if it doesn't already exists
    • place a file in there with a .pth extension, say my_venv.pth
    • write the line /Users/maartenidema/t_venv/lib/python3.6/site-packages into that file
    • Restart DrawBot and see if you can now import from your venv.

    Sorry, I didn't test this. Please let us know if it works.



  • Oh, I've been trapped by a bot it seems 😞 Still, my answer may be useful 🙂