0

I have a file "A" that contains function "a" that uses environment vars - "1","2","3"

I'm trying to call function "a" from file "B" by importing file "A" into file "B", but in order to import file "A", it must compile it, which cannot be done because the environment variables have not been set.

So my question is how do I import a file "A", while simultaneously setting up environment variables "1", "2", "3" so that "A" will compile and not break down.

Thanks

1
  • 1
    Show your code! Commented Dec 20, 2020 at 19:57

1 Answer 1

1

You can use os.environ, which first requires that you import os. os.environ is a dictionary that maps environment variable names to values. For example, os.environ['HOME'] would be equal to your home directory.

However, are you sure you are "compiling" file A? Python is generally interpreted, rather than compiled, which means you do not have to use a program to turn your code into an executable. I will assume you simply mean "import" when you say "compile".

The solution will be to first set the environment variables using os.environ[...] = ..., replacing the ellipses with the desired environment variable name (string) and value, respectively. Only after you set these variables should you import file A. Assuming you are running file B, the code in file A will only be interpreted when you import A. If you set the environment variables before importing, you should be good.

Caveat: Are you sure you want to set the variables in your code? It might make more sense to set them globally on your system, then add checks to file A to make sure they are set. It's not a good idea to assume environment variables are set.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.