如何在Python3环境执行Scons并导入Python3脚本?(v3.0.1.7)
Let’s break down your questions and walk through the solutions step by step:
Specifying Python 3 as the Interpreter for SCons
The scons -python /usr/bin/python3 syntax isn’t a supported flag in SCons. Instead, the most reliable way to force SCons to use Python 3 is to invoke the Python interpreter directly and pass the SCons entry point to it. Here’s how:
- If SCons is already installed for Python 3, you can run it via Python’s module system:
/usr/bin/python3 -m scons - If you’re working with a local SCons script or haven’t installed it for Python 3 yet:
/usr/bin/python3 /path/to/your/scons/install/scons.py
Alternatively, if you install SCons using Python 3’s package manager (pip3), the default scons command will automatically use Python 3 (assuming your system PATH prioritizes Python 3’s bin directory).
Building/Installing a Python 3-Compatible SCons v3.0.1.7
Since SCons v3.0.1.7 officially supports Python 3.5+, setting up a Python 3-compatible version is straightforward:
Option 1: Install via pip3 (Recommended)
This is the easiest method. Run the following command to install SCons specifically for your Python 3 environment:
pip3 install scons==3.0.1.7
After installation, the scons command will default to using Python 3.
Option 2: Build from Source
If you prefer to compile from the source code:
- Download the source archive for SCons v3.0.1.7 from the official release repository.
- Extract the archive and navigate to the source directory in your terminal.
- Run the installation using your target Python 3 interpreter:
/usr/bin/python3 setup.py install
This will build and install SCons using Python 3, ensuring full compatibility.
Verify Your Setup
To confirm SCons is using Python 3, run:
scons --version
Look for a line in the output like this (showing Python 3.5 or higher):
Python: 3.8.10
Importing Python 3 Scripts in Your SCons Script
Once SCons is running under Python 3, you can import your Python 3 scripts directly into your SConstruct or SConscript files just like regular Python code:
# In your SConstruct file import my_python3_script
If your script is in a non-standard directory, add its path to Python’s module search path first:
import sys sys.path.append("/path/to/your/python3/scripts") import my_python3_script
内容的提问来源于stack exchange,提问作者Chouettou

