Re: Project-wide variable...
On Jun 23, 9:41 am, Gnarlodious gnarlodi...@gmail.com wrote: Is there a way to declare a project-wide variable and use that in all downstream modules? -- Gnarlir What about using an environment variable? -- http://mail.python.org/mailman/listinfo/python-list
Re: Handling import errors
I did not think about using a global variable, and the top-level try...except solution is interesting. After further thinking, I have to reformulate my initial question: How do I manage to run code before my imports? For example, I want to make sure that I can use the logging module in the case an import fails, so I want to call logging.basicConfig() before this particular import. Likewise, I could want to import a module whose path is relative to an environment variable, and would want to test if this variable is set before doing so. I have come up with 2 solution templates : import logging main() def pre_import(): ... logging.basicConfig(format='%(message)s') def import(): ... global foo ... import foo def main(): ... pre_import() ... import() import logging logging.basicConfig(format='%(message)s') import foo main() def main(): ... pass To me, the latter looks better, but I could be missing something. In any case, surrounding the entire program with try...except would look like the following? try: ... import logging ... logging.basicConfig(format='%(message)s') ... import foo ... ... main() except Exception: ... # Display simple error message def main(): ... pass -- http://mail.python.org/mailman/listinfo/python-list
Handling import errors
What is the pythonic way to handle imports error? What is bugging me is that the imports can't be inside a function (because I use them in different places in the script and thus they have to be in the global scope). I would write something like: try: -- http://mail.python.org/mailman/listinfo/python-list
Handling import errors
What is the pythonic way to handle imports error? What is bugging me is that the imports can't be inside a function (because I use them in different places in the script and thus they have to be in the global scope). I would write something like: try: import foo except ImportError: logging.error('could not import foo') sys.exit(1) But logging is not configured at this point as my main() have not been called yet. Should I define a global variable and assign it to my module later? Or should I let the exception happen and let the stack trace be the error message? -- http://mail.python.org/mailman/listinfo/python-list
import from environment path
Hi, Here's my situation : I got a script a.py that need to call b.py. The 2 scripts can't be in a same package. Script a.py knows the path of b.py relative to an environment variable B_PATH, let's say B_PATH/foo/ b.py. The solution I found is to do the flowwing : b_dir = os.path.join(os.environ['B_PATH'], 'foo') sys.path.append(b_dir) import b b.main() Is it the right way to do it, should I use subprocess.call instead? -- http://mail.python.org/mailman/listinfo/python-list