Syntax error when importing a file which starts with a number

S

simon.woolf

Hello, all.

I don't suppose anyone has any idea why it seems to be impossible to
import any file which starts with a number? You get a syntax error,
whether the file exists or not.

Try it yourself:
ImportError: No module named foo
File "<stdin>", line 1
import 1foo
^
SyntaxError: invalid syntax

Is this just me, or has anyone else run into it? Is it a known bug?
(If so, I can't find it on a bug tracker or in any Google searches).

It's a bit annoying, as I have an enforced naming scheme. Any way
round it?

Thanks in advance!

Simon
 
M

Miles

Hello, all.

I don't suppose anyone has any idea why it seems to be impossible to
import any file which starts with a number?  You get a syntax error,
whether the file exists or not.

Identifiers can't start with a number.

http://docs.python.org/reference/lexical_analysis.html#identifiers
It's a bit annoying, as I have an enforced naming scheme.  Any way
round it?

You could import it like so:

some_valid_identifer = __import__('1foo', globals(), locals())

http://docs.python.org/library/functions.html#__import__

But a far better solution is to fix your naming scheme—it's completely
illogical to have a Python module naming scheme where the names aren't
valid identifiers.

-Miles
 
G

Gabriel Genellina

I don't suppose anyone has any idea why it seems to be impossible to
import any file which starts with a number? You get a syntax error,
whether the file exists or not.

You don't import a file, you import a module. And a module name is an
identifier:
http://docs.python.org/reference/simple_stmts.html#the-import-statement
Identifiers must begin with a letter or underscore.
(Standalone scripts that aren't supposed to be imported don't have to obey
this rule, like 2to3.py)
Is this just me, or has anyone else run into it? Is it a known bug?
(If so, I can't find it on a bug tracker or in any Google searches).

It's not a bug. Suppose you have a file '1.py' and you could write:

import 1
x = 1

Does x refer to the integer 1 or the module 1?
It's a bit annoying, as I have an enforced naming scheme. Any way
round it?

You might use a prefix in all these modules. Or just an _ in front of the
ones that start with a number. But remember that a module name must obey
both your filesystem rules for file names and Python rules for
identifiers: letters A-Z (unless you're using Python 3), digits, _.
 
M

MRAB

Hello, all.

I don't suppose anyone has any idea why it seems to be impossible to
import any file which starts with a number? You get a syntax error,
whether the file exists or not.

Try it yourself:

ImportError: No module named foo

File "<stdin>", line 1
import 1foo
^
SyntaxError: invalid syntax

Is this just me, or has anyone else run into it? Is it a known bug?
(If so, I can't find it on a bug tracker or in any Google searches).

It's a bit annoying, as I have an enforced naming scheme. Any way
round it?
A module name must be a valid identifier. For example, if you do:

import foo

then you can write:

foo.bar()

but if it allowed:

import 1foo

then you'd be stuck because you can't write:

1foo.bar()

You also aren't allowed to have spaces in a name, so no, it's not a bug.

I suppose that if there was a demand for this kind of thing then quoting
could be used for the filename:

import "1foo" as one_foo
 
J

John Machin

It's a bit annoying, as I have an enforced naming scheme.

Do you mean that some authority other than yourself is seriously
insisting that the names of source files *must* start with one or more
digits? What is the rationale for such a scheme?
 
S

simon.woolf

Many thanks to all for explanations. I'm going to take everyone's
advice and ignore the naming scheme (especially as, on rereading, the
naming scheme is apparently only mandatory if you're using C or Maple,
for some reason).

Thanks again.

Simon

(For those interested:
Do you mean that some authority other than yourself is seriously
insisting that the names of source files *must* start with one or more
digits? What is the rationale for such a scheme?

The relevent section from the manual is
What to submit if you used C or Maple on the PWF
The first few characters of each file name should give the project number,
with the dot replaced by a minus sign or underscore ( _ ). For example, all
the programs written for project 1.2 should have file names beginning with
1-2 or 1_2; for instance 1-2prog.c or 1_2equip.c...

As "project" and "submit" suggest, this is for education rather than
work, so a certain amount of educational bureaucracy is not
unsurprising. Also, it does then go on to state that if you're unable
to use that convention then you can just include a readme file that
explains what you've done.)
 
J

John Machin

Many thanks to all for explanations.  I'm going to take everyone's
advice and ignore the naming scheme (especially as, on rereading, the
naming scheme is apparently only mandatory if you're using C or Maple,
for some reason).

Thanks again.

Simon

(For those interested:


The relevent section from the manual is

Gulp ...

So in a sorted list of files, some of the project 1.2 files will
appear under 1-2xxxx and others miles away under 1_2xxxx ? And even if
the submitter is not so dopey, the submittee has two different
possibilities when looking for project 1.2's files? Brilliant! And
this is an educational institution ? Have they never heard of
directories? Time to upgrade from MS-DOS 1.0 to MS-DOS 2.0! And what's
wrong with a dot in a filename anyway?
 
S

simon.woolf

So in a sorted list of files, some of the project 1.2 files will
appear under 1-2xxxx and others miles away under 1_2xxxx ? And even if
the submitter is not so dopey, the submittee has two different
possibilities when looking for project 1.2's files? Brilliant! And
this is an educational institution ? Have they never heard of
directories? Time to upgrade from MS-DOS 1.0 to MS-DOS 2.0! And what's
wrong with a dot in a filename anyway?

It's not that bad, thankfully: you do submit into a dedicated username-
allocated directory, so the files from everyone don't go all into one
place.

Barely anything's done with the submitted files anyway: I think they
just run them through some software to make sure it's not a blatant
copy&paste of anyone else's. The actual marking is done on your
written report -- which (in addition to the electronic submission) has
to contain printed listings of all your programs.

Thanks again.

Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top