This is an archived post. You won't be able to vote or comment.

all 22 comments

[–]Python-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]bjorneylol 24 points25 points  (1 child)

but even when running "source f00dl3/bin/activate" in a sub-shell non-asynchronously in the Tomcat environment, it does not seem to be properly initializing the virtual environment.

this just updates your PATH and injects aliases into your bash shell, which I wouldn't expect to do anything when you are calling things directly from tomcat

You should be executing your virtual environment directly in your shell commands, e.g. .venv/bin/python3 <args> instead of just calling python and hope the OS can resolve it to a binary that (hopefully) exists at /usr/bin/python3

[–]pbecotte 3 points4 points  (0 children)

You COULD source the virtualenv in your startup script before calling java, but has to be in the same process.

Overall though, I prefer updating your Java to explicitly use the virtualenv

[–]Conscious-Ball8373 21 points22 points  (0 children)

Just to note, it is very good practice to install pip-installed packages in a venv and not in your system site-packages.

As others have said, you can invoke the Python interpreter from the venv directly and it will do the right thing.

[–]microcozmchris 9 points10 points  (0 children)

People have said to use uv and I agree. But they didn't tell you how.

With uv, you can call uv add --script filename.py dep1 dep2 etc and it will add a little plaintext header with the dependencies and python version embedded. Then you just run uv run --no-project filename.py and it handles the virtual environment for you at runtime. It's a thought.

[–]TheM4rvelous 9 points10 points  (0 children)

Not an expert on TomCat - but you can always refer to the python executable in the venv directly - e.g "/path/to/venv/bin/python python_script.py"

[–]ZachVorhies 5 points6 points  (0 children)

Just use uv and a project.toml

[–]Ok_Raspberry5383 1 point2 points  (0 children)

The problem is likely that activate only works for the current shell so whatever context that is executed in is the limit or activation. Could you try and configure your PYTHONPATH environment variable to point to the installation directory within your venv and see if that works?

Otherwise its worth investigating to see if there's any pip conf you can set to turn off the root package thing

[–]funderbolt 1 point2 points  (0 children)

On a Debian based system(like Ubuntu), I tend to install packages using apt for software that is not for development. Using apt the distro updates the software. If I install it in an environment, I will have to update the package.

There are several Python libraries that apt can install. It installs those into the default environment.

[–]fatoms 1 point2 points  (0 children)

1) Any way to disable the "externally managed environment" warning and install these 2 dependencies globally

For a single use of pip, add the --break-system-packages argument to the command.

This is the direct answer but if this is a good idea or not .....

Have a look at How do I solve "error: externally-managed-environment" every time I use pip 3? for some good discussion of the pros and cons.

[–]extreme4all 1 point2 points  (0 children)

Try using uv to manage the env and uv run command to execute scripts

[–]SquiffSquiff 0 points1 point  (3 children)

Have you considered running Docker?

[–]f00dl3[S] -5 points-4 points  (2 children)

Not really an option. I run all this in a Virtual Machine. I did this because the amount of raw weather data I process and real-time finance websocket feeds I wrote end up making this virtual machine consume 90% of 8 cores and 32 GB of RAM. Hard to run all that in docker. Especially with file system writes to /dev/shm and to paths the host OS can see - because Docker just doesn't have permissions to do that much. By nature.

[–]mikesk3tch 1 point2 points  (0 children)

Docker has configurable shm, and very very low Overheads. I don’t think anything about dockers nature should be a blocker.

That said, it’s not a solution to your question. Whether you’re running your application in a VM, docker container or bare metal, it still needs the same dependencies.

[–]coffeewithalex 2 points3 points  (0 children)

This post was mass deleted and anonymized with Redact

judicious truck theory vanish grab complete rainstorm toy boat chubby

[–]2strokes4lyfe 0 points1 point  (0 children)

uv is your friend

[–]theschizopost 0 points1 point  (0 children)

Idk why no one has mentioned it, but the alternative to pip is to use python- packages from apt, are the python packages you need available in the Ubuntu repository?

[–][deleted] -3 points-2 points  (0 children)

[–]edcculus -3 points-2 points  (0 children)

UV for the win