Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

python - Possible to prune unwanted packages using environment.yml file when building conda environment?

I am making a conda virtual environment using an environment.yml file. In my package I want to use opencv version 4.1, from conda-forge. It installs great using the following line in the yml file:

- conda-forge::opencv=4.1

Unfortunately another package I'm including as a dependency has opencv-python as a dependency. Hence, once the environment is resolved, you end up with both opencv (version 4.1) and opencv-python (version 4.5) installed. This leads to problems when running my code.

Luckily, when I manually uninstall opencv-python everything works just fine in my package.

The problem is I don't want my github repo install instructions to be like: "Create this virtual environment with environment.yml file. Oh BTW you then need to enter pip uninstall opencv-python or else you will run into problems."

Is there a way to take care of this last pruning step in my environment.yml file, so things will just work out of the box?

question from:https://stackoverflow.com/questions/65930856/possible-to-prune-unwanted-packages-using-environment-yml-file-when-building-con

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I suppose you could create your own conda package, which will be mostly empty except for a special activation script in ${PREFIX}/etc/conda/activate.d/. The activation script will check if opencv-python is installed, and remove it.

This recipe would do it:

recipe/
├── meta.yaml
└── uninstall-opencv-python.sh
# meta.yaml
package:
  name: my-hack-to-uninstall-opencv-python
  version: 0.1

build:
  number: 0
  noarch: generic
  script:
   - mkdir -p {{ PREFIX }}/etc/conda/activate.d/
   - cp {{ RECIPE_DIR }}/uninstall-opencv-python.sh {{ PREFIX }}/etc/conda/activate.d/
# uninstall-opencv-python.sh
if (pip list | grep opencv-python > /dev/null); then
   echo "Uninstalling opencv-python"
   pip uninstall opencv-python
fi

Build that recipe and upload the resulting package to your own channel on anaconda.org. Then, add a line to your environment.yml file:

- mychannel::my-hack-to-uninstall-opencv-python

The only downside is that it will run that script every time the environment is activated. But it's a relatively fast script, so maybe that's not a problem. Obviously any solution to this particular problem will be somewhat of a hack, no matter what you come up with.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...