如何在cocotb中强制使用python 3?

How to force usage of python 3 in cocotb?

我正在使用 CocoTB 来测试我的 HDL 设计,但据我了解,它可以与 python2.7 或 python3.

一起使用

在 setup.py 配置文件中,我可以看到两者都受支持:

    [...]
    "Programming Language :: Python :: 2.7",
    "Programming Language :: Python :: 3",
    [...]

在endian_swapper测试(examples/endian_swapper/tests/test_endian_swapper.py)中,如果我修改一个测试脚本,看看用的是哪个版本:

@cocotb.test()
def wavedrom_test(dut):
    """
    Generate a JSON wavedrom diagram of a trace and save it to wavedrom.json
    """
    print("Python version used {}".format(sys.version_info))

我可以看到当我使用 «make» 命令启动测试时使用 python2.7 :

Python version used sys.version_info(major=2, minor=7, micro=9, releaselevel='final', serial=0)

我的 python3 可执行文件名为 ... python3 事实上 (debian)。有没有一种规范的方法来强制 cocotb 使用 python3 而不是 python2 ?

我在 linuxconfig.org, thanks to themperek 上找到了解决方案。但这并不是我想要的。

别名解决方案对我不起作用。 update-alternative 有效,但只适用于 debian 上安装的“官方”python3。我无法使用手动安装的替代 (3.7)。

$ sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.7   2         auto mode
  1            /usr/bin/python2.7   0         manual mode
  2            /usr/bin/python3.4   1         manual mode
* 3            /usr/bin/python3.7   2         manual mode

Press enter to keep the current choice[*], or type selection number: 3
$ make clean;make
0.00ns INFO     Running on Icarus Verilog version 11.0 (devel)
0.00ns INFO     Python interpreter initialised and cocotb loaded!
0.00ns INFO     Running tests with Cocotb v1.0.1 from /opt/cocotb
0.00ns INFO     Seeding Python random module with 1554105931
0.00ns INFO     Found test test_ttl.ttl_test
0.00ns INFO     Running test 1/1: ttl_test
0.00ns INFO     Starting test: "ttl_test"
                Description:  simple ttl test function 
[...]
3.4.2 (default, Feb  7 2019, 06:11:23) 
[...]

我找到了正确的方法。

先在官网下载python最新版本:

$ wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz

然后展开并配置它 使用选项 --enable-shared

$ tar -Jxvf Python-3.7.4.tar.xz
$ cd Python-3.7.4
$ ./configure --enable-shared
$ make
$ sudo make install

安装完成后,转到你的cocotb测试目录,然后安装虚拟环境:

$ export LD_LIBRARY_PATH=/usr/local/lib
$ virtualenv --python=/usr/local/bin/python3.7 envp37
$ source envp37/bin/activate
$ python -m pip install cocotb

然后你可以用传统的 make 启动你的 cocotb 测试环境:

$ make

激活 python 环境:

$ deactivate

尝试更新 PATH 变量。为我工作。
当 cocotb 查找 python 时,它会在 PATH 变量中列出的文件夹中查找它。
假设您的 python3 完整路径位于
/usr/bin/python3
(你可以通过which python3找到python3的完整路径)
我还在此处添加 link 到新位置,以防 python2 和 python3 位于同一文件夹中...

> python -V
Python 2.7.17
> ln -s /usr/bin/python3 /home/$USER/python
> export PATH="/home/$USER:$PATH"
> python -V
Python 3.6.9