Clockmender / Clockworx-Music

A set of Nodes and New Node Editor to produce Music from Animations and Animations from Music
GNU General Public License v3.0
27 stars 2 forks source link

Installing PyGame - A generic, cross platform solution. #4

Closed chiaravalle closed 3 years ago

chiaravalle commented 3 years ago

You can install pip modules from the Blender's Python console, by just writing 2 lines of code:

import pip (hit enter)
pip._internal.main(['install', 'pygame']) (hit enter)

Credits: https://stackoverflow.com/questions/12332975/installing-python-module-within-code Tested on Blender 2.93 with Windows 10.

Cheers B)

Clockmender commented 3 years ago

Thank you, I will update the Wiki to this effect!

I do like this version:

import subprocess
import sys

try:
    import pygame as pg
except ImportError:
    subprocess.check_call([sys.executable, "-m", "pip", "install", 'pygame'])
finally:
    import pygame as pg

Which presumably I could add to the scripts where PyGame is called or the __init.py__ file? I'll have a play and see what happens...

Cheers, Clock.

PS. sorry for delay in replying...

chiaravalle commented 3 years ago

No problem for the delay. Yes, your version seems more appropriate, as it will install pygame the first time the AddOn is launched.

Clockmender commented 3 years ago

OK, pip is not installed in a new install...

So if I ensure pip first, it works, is there a way to ensure pip in the init.py file?

chiaravalle commented 3 years ago

Dunno, sorry. I made the script after StackOverflow, but I know nothing about Python

Clockmender commented 3 years ago

No Problem, I'll play with it!

Clockmender commented 3 years ago

You do it like this:

import subprocess
import sys

try:
    import pip
except ImportError:
    subprocess.check_call([sys.executable, "-m", "ensurepip"])
try:
    import pygame as pg
except ImportError:
    subprocess.check_call([sys.executable, "-m", "pip", "install", 'pygame'])
finally:
    import pygame as pg