0

I have a code in production that requires to read certain modules if a parameter is given 1 and should not read certain modules if a parameter is zero. Here is a simple example:

global parameter
if parameter==0: 
   import numpy as np 

if parameter ==1:
   import pandas as pd

Is there anyway to achieve this in python? I cannot import both modules at the same time.

user2512443
  • 485
  • 1
  • 6
  • 20
  • Yes, you can. Only you have to keep in mind to indent the code accordingly –  Jul 27 '21 at 02:09
  • can you please give an example? Does it work the way I write in my question? I thought i could not import like that – user2512443 Jul 27 '21 at 02:11
  • When I copy and paste your code, it works. What problem are you having with it? – Mark Jul 27 '21 at 02:13
  • 1
    See [Conditional import of modules in Python](https://stackoverflow.com/questions/3496592/conditional-import-of-modules-in-python) – smci Jul 27 '21 at 02:27
  • Importing Pandas will cause Numpy to be imported behind the scenes, FWIW. – Karl Knechtel Jan 19 '23 at 04:21

1 Answers1

1

The code you have here must work properly as is.

jalovisko
  • 126
  • 3