Using pip in a container: running as root or using virtual environments

I resolved the space issue using the ‘docker system prune -a -f’ command in Docker. However, a new problem has arisen. While I can successfully create a new container and install Python inside it, I’m facing issues when trying to import modules like cv2, torch, etc. I started seeing a warning message:

‘WARNING: Running pip as the ‘root’ user can result in broken permissions and conflicting behavior with the system package manager. It is recommended to use a virtual environment instead.’

I could use a virtual environment, but it worked fine without one before. Also, I installed the latest version of Python (as I did before).

and i tried ‘pip install opecv-python’ too

Does anyone know how to resolve this issue?

I also emptied the trash bin…

I’d be truly grateful to anyone who can help.

The future of our lab depends on this.

I am not sure how python problems are related to the topic title. A wrong or not specific enough topic title can have the effect that people who might know the answer to your problem, don’t even look at it.

From my perspective it looks like a python problem, which just happen to happen in a container, but would also happen inside a vm or on the host system.

You should ask the python experts in your team, they should know what to do.

I am not a python guy, so I can’t tell you what needs to be done.

I agree with @meyay regarding the topic title so I changed it for you, because recently someone recommended this post about Python and Pip in a container

The warning message you noticed in the container was always there as far as I know. Maybe it depends on the Python or pip version, but I think I have seen it even about two years ago. You could ignore it or use -q to run pip in quiet mode which hides warnings and deal with packages if running it as root causes problems. Although using Python is not the scope of this forum, we can still discuss the best practices (or good practices) in containers.

So the question could be: why would we run a virtual environment in a container when the container is already an isolated environment so you would install only packages you wanted for your app. To answer it, it would require someone who knows a lot about Python and pip and also Docker. I knew some about Python and pip, but this blog post was interesting from someone who probably knows it better.

The post also recommends alternatives to the usual virtual environments.

thank you ! I will! have a good day

okay i wil try! thank you. have a good day!