Open nickfraser opened 2 years ago
Thanks for your detailed report!
Yes, it is on purpose. It is a hard decision. The reason is onnxruntime has at least three packages on PyPI -- onnxruntime, onnxruntime-gpu, and onnxruntime-noopenmp. The ideal behavior is to install onnxruntime only if none of these packages is installed. It is not supported by pip, so I have to install onnxruntime in the runtime because then I can check if any of these packages is installed by try-catch import onnxruntime
.
Since your virtual environment doesn't search your local user prefix (~/.local) for packages, the import fails anyway**
It is a little strange. onnxsim invokes pip by sys.executable -m pip
so that the virtual environment should be respected. However, I admit the current way is tricky and perhaps cannot work well in some corner cases. I'm willing to change it if there is a better way.
I strongly recommend you report an error if onnxruntime cannot be imported rather than trying to install it automatically.
How about reporting an more explicit and clear error if onnxruntime cannot be imported even after automatical installation? Feel free to share your idea :)
Apologies for the delay, I missed the notification that you responded to this.
The reason is onnxruntime has at least three packages on PyPI -- onnxruntime, onnxruntime-gpu, and onnxruntime-noopenmp.
I see, thanks for this information. I think it's reasonable not to include onnxruntime
in the pip
dependencies for those reasons.
onnxsim invokes pip by
sys.executable -m pip
so that the virtual environment should be respected.
I think you are incorrect on this. You're also adding the --user
flag which doesn't have clearly defined behaviour within virtual environments. See this comment. This answer also warns of the same behaviour.
Maybe an alternative solution is to simply update your installation documentation. Stating that a version of onnxruntime
is a prerequisite. For example, the python installation instructions could be changed to read as follows:
pip3 install -U pip
pip3 install <onnxruntime|onnxruntime-gpu|onnxruntime-noopenmp> # Install the onnxruntime of your choice.
pip3 install onnxsim
instead of:
pip3 install -U pip && pip3 install onnxsim
How about reporting an more explicit and clear error if onnxruntime cannot be imported even after automatical installation? Feel free to share your idea :)
I still strongly recommend you remove the automatic installation. Just print an error saying:
No onnxruntime found. Please install onnxruntime. This can be done as follows:
pip3 install <onnxruntime|onnxruntime-gpu|onnxruntime-noopenmp>
This way a user can decide whether or not they use a --user
flag or not. I don't think there is a clean way to do what you want to do without creating a bigger mess under some other configurations.
With conda maybe you want to update your library path?
You can locate the installation with:
$ pip show [some package]
...
Location: /path/to/conda/anaconda3/envs/[env]/lib/...
...
Then update the library path with:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/conda/anaconda3/envs/[env]/lib
Love this tool, but it has some weird behaviour on the first import that I think should be addressed.
The Issue
Seemingly, this was done on purpose, but I cannot understand why, so I'm bringing it to your attention. Installing
onnxsim
(version 0.4.7) withpip
doesn't installonnxruntime
as a prerequisite*. Instead, it checks / installs all other dependencies and installsonnxruntime
the first time you runfrom onnxsim import simplify
.The problem is, if you're using a virtual environment (such as
conda
) it will installonnxruntime
in~/.local/
instead of your chosen prefix. If that's the case, the following two things may occur:~/.local
) for packages, the import fails anyway**onnxruntime
and it's dependencies usingpip
Unless I'm missing something, I strongly recommend you report an error if
onnxruntime
cannot be imported rather than trying to install it automatically. I also recommend that you includeonnxruntime
as a prerequisite foronnxsim
so that it is installed bypip
whenonnxsim
is installed, so it is installed into the correct prefix.Steps to Reproduce the Issue
If you have something like miniconda installed (I happen to be using miniforge):
Logs
onnxsim
. Note that onnxruntime is not installed:** Importing
simplify
.onnxruntime
is installed in the user prefix, but fails anyway: