Farhood Etaati
05/27/2023, 10:42 AMNate
05/27/2023, 6:07 PMIn [2]: import subprocess
...: import venv
...: import shutil
...: from contextlib import contextmanager
...:
...: from prefect.logging.loggers import get_logger
...:
...: logger = get_logger()
...:
...: @contextmanager
...: def temp_venv():
...: venv_dir = 'temp_venv'
...: <http://logger.info|logger.info>('Creating virtual environment...')
...: venv.create(venv_dir, with_pip=True)
...: venv_python = f'{venv_dir}/bin/python'
...: try:
...: yield venv_python
...: finally:
...: <http://logger.info|logger.info>('Deleting virtual environment...')
...: shutil.rmtree(venv_dir)
...: <http://logger.info|logger.info>('Virtual environment deleted.')
...:
...: # could call this part within a task if you want
...: with temp_venv() as venv_python:
...: # Perform operations inside the virtual environment
...: <http://logger.info|logger.info>('Installing numpy...')
...: subprocess.run([venv_python, '-m', 'pip', 'install', 'numpy']) # -r requirements.txt
...: <http://logger.info|logger.info>('numpy installed.')
...:
...: # And run a script
...: <http://logger.info|logger.info>('Running script...')
...: subprocess.run([venv_python, 'my_script.py'])
...: <http://logger.info|logger.info>('Script completed.')
Robert Banick
05/28/2023, 5:07 PMEXTRA_PIP_PACKAGES
environment variable and forcing AWS to install whatever was set there. Apparently Prefect’s entrypoint.sh
script will automatically do this for non-AWS environments so setting the variable may be enough. Good luck!Nate
05/28/2023, 5:12 PMEXTRA_PIP_PACKAGES
would work if you wanted to have a different set of dependencies per container or something but OP said
the overhead is too much both from networking PoV and also resource wise
Robert Banick
05/28/2023, 5:14 PMNate
05/28/2023, 5:17 PMis it necessary to use a temp_venv like you do in the above example when installing packages within a flow?no i dont think its necessary to do what I did above, I'm sure there are other ways to do it. i didn't thoroughly test that solution, its just similar to something thats worked for me before
Robert Banick
05/28/2023, 5:20 PMNate
05/28/2023, 5:20 PM