0

I try to import negated from 03_a_ImportPredefinedWordLists.py (where negated along with some other functions is stored). I tried what is suggested in this post

from 03_a_ImportPredefinedWordLists import negated

Python complains about syntax error because the .py starts with a number.

 File "<ipython-input-12-fc574baf5a3f>", line 1
    from 03_a_ImportPredefinedWordLists import negated
           ^
 SyntaxError: invalid token

If I rename the .py file to ImportPredefinedWordLists, it works fine. Given there are many .py files, I numbered them, to keep track. I would not like to change that unless I have to. Is there a "workaround". I tried to escape using \ but could not get it working

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
safex
  • 2,398
  • 17
  • 40
  • 2
    Having a file name starting with a number is asking for trouble... https://stackoverflow.com/questions/9090079/in-python-how-to-import-filename-starts-with-a-number – jbat100 Mar 28 '19 at 11:11
  • 1
    Actually come from the fact that variable names should not start with a number https://stackoverflow.com/questions/41962391/variable-names-in-python-cannot-start-with-a-number-or-can-they – jbat100 Mar 28 '19 at 11:20
  • after reading some more, I agree. one can easily circumven the problem by adding some letter before all numbers, preserving the order and readability of the file names. I will leave the question online and accept the answer that best fits the original task or importing a function from a file starting with a number, in case anyone else faces the same issues and cannot (for some reason) rename the files – safex Mar 28 '19 at 12:20

0 Answers0