16

I need to import python3 scripts within a scons script. (I use scons v3.0.1.7)

Is there a way to execute scons with python 3 ?

The offical scons site says

This will be the first release to support Python versions earlier than 2.7 as well as 3.5+

Something like scons -python /usr/bin/python3 would be good to select the python interpreter.

I did not find either how to build a scons version for python3.

Chouettou
  • 1,009
  • 1
  • 9
  • 10
  • 1
    Related: https://stackoverflow.com/questions/44096321/compile-scons-in-python3 and https://stackoverflow.com/questions/32135869/managing-python-3-code-with-scons – dirkbaechle Jan 10 '18 at 13:19
  • 3.0.1.7 is not an official SCons version, 3.0.1 is so this is your distro's packaging. – bdbaddog Jan 12 '18 at 03:23

4 Answers4

14

In the latest versions, SCons supports both Python2 and Python3. There is no need to recompile SCons itself or such. All that matters is which Python version is the default on your system.

You don't mention under which OS you're trying to get this working, so I assume a Linux for now. After the installation you can find the scons script in a location like /usr/local/bin/scons (try "which scons" if in doubt). The first line

#! /usr/bin/env python

tells your system to use the default Python version. If this is not what you want, you can simply change it to e.g.

#! /usr/bin/env python3

if you're under a Python2-default system, but want to run SCons with Python3 instead.

dirkbaechle
  • 3,984
  • 14
  • 17
  • I am on macOS. Thank you, it works well! I edited the scons executable as you suggested. But maybe it is safer to create a "scons3" from the original one and then change it. I use homebrew which will certainly overwrite it on next update. – Chouettou Jan 11 '18 at 15:41
  • Python2 support has been dropped. – Andreas Oct 04 '20 at 11:12
14

Here is a better solution, add this to your .bash_profile:

alias scons3="/usr/bin/env python3 $(which scons)" 
Chouettou
  • 1,009
  • 1
  • 9
  • 10
  • Also useful when you want to force Python 2.x for a newer scons which wants Python 3. (yes, I know Python 2 is EOL; trying to fix one thing at a time here) – Roger Lipscombe Sep 29 '20 at 11:27
3

Assuming you have python 3 virtualenv installed:

virtualenv-3.6 venv3
venv3/bin/pip install -U setuptools wheel pip
venv3/bin/pip install scons
venv3/bin/scons

That allows your install to be unaffected by homebrew's updates..

bdbaddog
  • 3,357
  • 2
  • 22
  • 33
3

I had to edit /usr/bin/scons and change the shebang from

#! /usr/bin/python

to

#! /usr/bin/env python

Make sure you are in a python3 virtual environment

Timothy Pulliam
  • 132
  • 1
  • 9
  • 25