iewnfod / CAIE_Code

An Interpreter for CAIE Pseudocode
https://muyunxi.top/cpc
Mozilla Public License 2.0
27 stars 7 forks source link

Add Python venv #96

Closed Anson2251 closed 3 weeks ago

Anson2251 commented 3 weeks ago

问题描述

I tried to use the CAIE_Code_Installer.dmg file to install the cpc. But my python was installed by brew and the environment is managed by it. In this way, cpc cannot install all the dependencies it needs. When I run cpc, python shows the following error.

Missing Important Dependence `ply`
Trying to Install for You...
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.

    If you wish to install a Python library that isn't in Homebrew,
    use a virtual environment:

    python3 -m venv path/to/venv
    source path/to/venv/bin/activate
    python3 -m pip install xyz

    If you wish to install a Python application that isn't in Homebrew,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. You can install pipx with

    brew install pipx

    You may restore the old behavior of pip by passing
    the '--break-system-packages' flag to pip, or by adding
    'break-system-packages = true' to your pip.conf file. The latter
    will permanently disable this error.

    If you disable this error, we STRONGLY recommend that you additionally
    pass the '--user' flag to pip, or set 'user = true' in your pip.conf
    file. Failure to do this can result in a broken Homebrew installation.

    Read more about this behavior here: <https://peps.python.org/pep-0668/>

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
...

I tried to use brew to install these dependencies, but it seems ply has been disabled by brew.

$ brew install python-ply
Error: python-ply has been disabled! It will be disabled on 2024-08-15.

复现方法

  1. use brew to install python3
  2. install cpc via CAIE_Code_Installer.dmg
  3. run cpc

版本信息

cpc v0.1.4 Python 3.12.4 Darwin 23.6.0

额外说明

With my limited programming experience with Python, venv can be used to solve this problem, as it provides a virtual environment and brew won't lock it.

I tried setting up a venv with the following command.

cd /usr/local/sbin/CAIE_Code
python3 -m venv ./

By the way, the venv have to be activated before running cpc.

source /usr/local/sbin/CAIE_Code/bin/activate

If venv can be set up before the first running, it can solve the problem almost perfectly, except for one small flaw, which is the manual execution of the activation command.

A patch can be applied to the /bin/cpc to activate the venv before running.

@@ -62,6 +62,7 @@
        parent_path=$parent_path/..
        script_path=$parent_path/main.py
 done
+source "$parent_path/bin/activate"
 unset -v parent_path
 unset -v exe_path

Thanks in advance for your attention to this matter!

lightumcc commented 3 weeks ago

Does your python installed via Homebrew? If yes, please replace it with a package that support third-party pip package installed. FYI, pkg on python.org is a good choice, or use brew install pypy3.10 as an alternative

lightumcc commented 3 weeks ago

It is not recommanded you to manage virtual environment by your own, and we can not provide support with the venv.

Anson2251 commented 3 weeks ago

use brew install pypy3.10 as an alternative

Thanks! That fix the problem. Btw, may I ask, what are the drawbacks of using a virtual environment for cpc?

lightumcc commented 3 weeks ago

Well... Actually we never test our software in a virtual environment. This is the only drawback😂.

Maybe you can test it and provide a best practice.👍

lightumcc commented 3 weeks ago

We have already made changes inout latest installation method which depends on Homebrew completely.