I use python-shell-send-buffer-switch
it's provided by the python layer in spacemacs (python-mode I assume?). It's bound to , s B
and opens and runs your code in a python shell. You can choose python or python3 by customize the variable python-shell-interpreter