All-numeric script names and import

C

Chris Angelico

If I have a file called 1.py, is there a way to import it? Obviously I
can't import it as itself, but in theory, it should be possible to
import something from it. I can manage it with __import__ (this is
Python 2.7 I'm working on, at least for the moment), but not with the
statement form.

# from 1 import app as application # Doesn't work with a numeric name
application = __import__("1").app

Is there a way to tell Python that, syntactically, this thing that
looks like a number is really a name? Or am I just being dumb?

(Don't hold back on that last question. "Yes" is a perfectly
acceptable answer. But please explain which of the several
possibilities is the way I'm being dumb. Thanks!)

ChrisA
 
M

Mark H Harris

# from 1 import app as application # Doesn't work with a numeric name
application = __import__("1").app

Is there a way to tell Python that, syntactically, this thing that
looks like a number is really a name? Or am I just being dumb?

(Don't hold back on that last question. "Yes" is a perfectly
acceptable answer. But please explain which of the several
possibilities is the way I'm being dumb. Thanks!)

If you have a script that is self-programming (producing sequenced,
numbered scripts 1.py 2.py 3.py) then why not just prefix an alpha
character a1.py a2.py a3.py ?

Otherwise, are you just pulling our chain? :)


On the other hand, if you open IDLE and then open the 1.py module (yes,
that's a dumb name) and then click run--> run module it will import and
run... assuming 1.py contains some valid python code.


Why must you have a numbered script?



You're just pulling someone's chain, right?



marcus
 
M

Mark H Harris

# from 1 import app as application # Doesn't work with a numeric name
application = __import__("1").app

Is there a way to tell Python that, syntactically, this thing that
looks like a number is really a name? Or am I just being dumb?

(Don't hold back on that last question. "Yes" is a perfectly
acceptable answer. But please explain which of the several
possibilities is the way I'm being dumb. Thanks!)

If you have a script that is self-programming (producing sequenced,
numbered scripts 1.py 2.py 3.py) then why not just prefix an alpha
character a1.py a2.py a3.py ?

Otherwise, are you just pulling our chain? :)


On the other hand, if you open IDLE and then open the 1.py module (yes,
that's a dumb name) and then click run--> run module it will import and
run... assuming 1.py contains some valid python code.


Why must you have a numbered script?



You're just pulling someone's chain, right?



marcus
 
C

Chris Angelico

On the other hand, if you open IDLE and then open the 1.py module (yes,
that's a dumb name) and then click run--> run module it will import and
run... assuming 1.py contains some valid python code.

Oh, it runs fine as an application, just not importably.
Why must you have a numbered script?

You're just pulling someone's chain, right?

Heh. No, I'm actually finally getting around to rewriting something in
Python. It's been called 1 for as long as it's ever existed, having
made the jump from a flat file in my personal directory to a web site,
and since then from MySQL to PostgreSQL, and finally now I'm getting
rid of the last artefact of the old web host by ditching PHP. Yes, I
wrote it in PHP because it was hosted on a server that didn't support
Python, and when I moved everything onto my own server, I didn't
rewrite it. But that's no excuse for changing the name now :)

http://rosuav.com/1/

Now, I could of course rename the .py files while still having it at
/1/ on the site, but that's cheating. :)

ChrisA
 
R

Rustom Mody

If I have a file called 1.py, is there a way to import it? Obviously I
can't import it as itself, but in theory, it should be possible to
import something from it. I can manage it with __import__ (this is
Python 2.7 I'm working on, at least for the moment), but not with the
statement form.


$ cat ا.py
x = 1
def foo(x): print("Hi %s!!" % x)



$ python3
Python 3.3.5 (default, Mar 22 2014, 13:24:53)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.1
 
C

Chris Angelico

Latin, you DID use Arabic numbers :)

I may have used an Arabic numeral, but I named my script very
definitely in English. Isn't it obvious? It's read "one dot pie",
which is clearly English! :)

ChrisA
 
W

Wolfgang Maier

I may have used an Arabic numeral, but I named my script very
definitely in English. Isn't it obvious? It's read "one dot pie",
which is clearly English! :)

I see, so what you should propose then is a change to import, so that
when it can't find a module it will try to import an alternative that's
pronounced the same way. Then you could simply do:

import one

and you're fine :)

Cheers,
Wolfgang
 
C

Chris Angelico

I see, so what you should propose then is a change to import, so that when
it can't find a module it will try to import an alternative that's
pronounced the same way. Then you could simply do:

import one

and you're fine :)

This strikes me as a very dangerous proposal. Imagine what would
happen when you try to "import chip" in New Zealand and it goes out
looking for "chup.py". Or the devastating results of "yes.py" and
"no.py" being indistinguishable on politicians' builds of Python...

ChrisA
 
M

Michael Torrie

This strikes me as a very dangerous proposal. Imagine what would
happen when you try to "import chip" in New Zealand and it goes out
looking for "chup.py". Or the devastating results of "yes.py" and
"no.py" being indistinguishable on politicians' builds of Python...

If you use a proper web framework, then you can define your urls to be
anything you want. Just set up the appropriate mapping and away you go.

Furthermore you can use apache directives to alias /1/ to something more
importable.
 

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

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,730
Latest member
AudryNolan

Latest Threads

Top