I want to do something like this in C:
#ifdef SOMETHING
do_this();
#endif
But in Python this doesn't jive:
if something:
import module
What am I doing wrong? Is this possible in the first place?
It should work fine:
>>> if False:
... import sys
...
>>> sys
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
>>> if True:
... import sys
...
>>> sys
<module 'sys' (built-in)>
If you're getting this:
NameError: name 'something' is not defined
then the problem here is not with the import statement but with the use of something, a variable you apparently haven't initialized. Just make sure it's initialized to either True or False, and it'll work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With