Hi, does anyone know if there’s a way to prevent o...
# ask-metaflow
c
Hi, does anyone know if there’s a way to prevent one’s local environmental variables ending up in flow steps when starting Flows from a local terminal? I’m using the
@environment
decorator to set values, but I found that my personal credentials (in my local terminal environment) were available in Metaflow.
1
v
you can empty
os.environ
in your own code (e.g in a custom decorator) prior to execution of any other custom code. Would that work?
c
Yeah, that would work. I’m curious if that was a deliberate design choice- is it a more common use case for users to want all variables available within Metaflow?
It seems like, from a standpoint of reproducibility, this could lead to some problems when things are implicit
Does metaflow log all these values?
v
yes, it’s deliberate. It’s common to have all kinds of credentials and other settings as we vars. Hiding them by default would cause friction especially during prototyping you are right that reproducibility would benefit from a stricter approach. The philosophy is to make it easy to get started and allow projects to be hardened gradually through features like
@secrets
Metaflow doesn’t log all env vars by default. You can do
self.environ = os.environ
optionally
here's an example of a custom decorator,
@strict_environment
that makes sure you get a clean environment with only the env vars you specify +
USER
that Metaflow needs internally
it also logs the original environment in an artifact,
self.original_environment
c
Thanks for the explanation. Are virtual environments also packaged and shipped off as part of the flow? I thought it was only subdirectories of the Flow file. Should I expect that packages installed in my local venv (from which I trigger the flow) will be available in the dockerized Step?
BTW, I think the above wrapper is incompatible with
@secrets
. I was unable to make them play nicely together, regardless what order I put the decorators.
v
re: virtual env - everything under the directory hierarchy where the flow file is stored is packaged automatically. You need to include packages you want to include by default in the directory hierarchy by hosting the package in the same repo, by symlinking, or by using
git subtree
etc. In addition or alternatively, you can use
@conda
to include packages
c
In order to support an internal company package, I’ve cloned this library into a
dependencies
folder in the same repo (but in a parent directory) of my flow. Based on the documentation, this would not be present in the K8s containers for each step.
I
pip installed
it with the
--editable
argument, so it’s available in my venv where I’m triggering a Flow onto OBP
As discussed earlier, environmental variables from my local machine are being made available to the docker container. I understand the reasoning there and am working around it. However, I’m also getting the local copy of this dependency (when I make changes on my local machine) they’re also made in the docker container
I would like to clone and install this library from github, however, it’s pulling my local version instead (despite it being in the parent directory)
v
do you want to always use the Github version and never use the local version?
c
I at least want to be able to choose
At the moment, I’m actually performing a clone and install within the Step, but that’s not the version being used
It’s just weird that sometimes it uses my local venv within a Step, and sometimes it doesn’t
v
re: choosing, one approach could be:
Copy code
if self.development_mode:
   from experimental.my_package import my_module
else:
   from subprocess import check_call
   check_call([sys.executable, '-m', 'pip', 'install', 'my_package'])
   from my_package import my_module
now if you had a directory structure like this
Copy code
flow.py
experimental/
   my_package/
     my_module.py
you could use a parameter
development_mode
to choose between the experimental local version or installing a clean upstream version on the fly
c
Sure
As far as the bigger question goes, is Metaflow attempting to use packages from the venv I’m running on my local?
v
yes, by default it uses all packages available in the environment like any python script or notebook. If you want to control the environment, you need to use
@conda
. Even if you don't install any Conda packages, you can just add an empty
@conda
and then
pip
install packages on the fly to get a clean environment.
(in a few months you'll be able to do the same with
@pip
)
h
@straight-shampoo-11124 how can I install an in house package like this: pip install git+https://github.com/bla/amazingstuff.git using @conda ? ideally flow wide to be able to run flow in cloud? I have this at the moment:
Copy code
@conda_base(
    libraries={
        "numpy": "1.26.4",
        "pandas": "2.2"
    },
    python="3.11.9"
)
v