ValueError: zero length field name in format - Running under Python 2.7.3?

Discussion in 'Python' started by Victor Hooi, Nov 4, 2013.

  1. Victor Hooi

    Victor Hooi Guest

    Hi,

    I have a Python script that's using a format string without positional specifiers. I.e.:

    LOG_FILENAME = 'my_something_{}.log'.format(datetime.now().strftime('%Y-%d-%m_%H.%M.%S'))

    I'm running this from within a virtualenv, running under Python 2.7.3.

    $ python -V
    Python 2.7.3
    $ which python
    /opt/my_project_venv/bin/python

    The first line of the script is:

    #!/usr/bin/env python

    However, when I run this line, I get the following error:

    Traceback (most recent call last):
    File "my_script.py", line 25, in <module>
    LOG_FILENAME = 'my_something_{}.log'.format(datetime.now().strftime('%Y-%d-%m_%H.%M.%S'))
    ValueError: zero length field name in format


    The weird thing, when I start a Python REPL and run that line interactively, it works fine:

    $ python
    Python 2.7.3 (default, Jan 7 2013, 11:52:52)
    [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from datetime import datetime
    >>> LOG_FILENAME = 'my_project_{}.log'.format(datetime.now().strftime('%Y-%d-%m_%H.%M.%S'))
    >>> print(LOG_FILENAME)

    my_project_2013-05-11_09.29.47.log

    My understanding was that in Python 2.7/3.1, you could omit the positional specifiers in a format string.



    Cheers,
    Victor
    Victor Hooi, Nov 4, 2013
    #1
    1. Advertising

  2. Re: ValueError: zero length field name in format - Running underPython 2.7.3?

    On Tue, Nov 5, 2013 at 9:33 AM, Victor Hooi <> wrote:
    > However, when I run this line, I get the following error:
    >
    > Traceback (most recent call last):
    > File "my_script.py", line 25, in <module>
    > LOG_FILENAME = 'my_something_{}.log'.format(datetime.now().strftime('%Y-%d-%m_%H.%M.%S'))
    > ValueError: zero length field name in format
    >
    >
    > The weird thing, when I start a Python REPL and run that line interactively, it works fine


    Google tells me that that was an issue in Python 2.6, so my first
    check would be to see what `/usr/bin/env python` actually gives you -
    are you running inside an environment that changes your path? Drop a
    "import sys; print(sys.version)" at the top of your script and see
    what it's really running as.

    ChrisA
    Chris Angelico, Nov 4, 2013
    #2
    1. Advertising

  3. Victor Hooi

    Victor Hooi Guest

    Re: ValueError: zero length field name in format - Running underPython 2.7.3?

    Hi,

    You're right - it was sudo playing up with the virtualenv.

    The script was in /opt, so I was testing with sudo to get it to run.

    I should have setup a service account, and tested it with that =).

    $ python sync_bexdb.py
    2.7.3 (default, Jan 7 2013, 11:52:52)
    [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)]

    $ sudo python sync_bexdb.py
    [sudo] password for victor:
    2.6.6 (r266:84292, Jul 10 2013, 22:48:45)
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]

    Cheers,
    Victor

    On Tuesday, 5 November 2013 10:02:50 UTC+11, Chris Angelico wrote:
    > On Tue, Nov 5, 2013 at 9:33 AM, Victor Hooi <> wrote:
    >
    > > However, when I run this line, I get the following error:

    >
    > >

    >
    > > Traceback (most recent call last):

    >
    > > File "my_script.py", line 25, in <module>

    >
    > > LOG_FILENAME = 'my_something_{}.log'.format(datetime.now().strftime('%Y-%d-%m_%H.%M.%S'))

    >
    > > ValueError: zero length field name in format

    >
    > >

    >
    > >

    >
    > > The weird thing, when I start a Python REPL and run that line interactively, it works fine

    >
    >
    >
    > Google tells me that that was an issue in Python 2.6, so my first
    >
    > check would be to see what `/usr/bin/env python` actually gives you -
    >
    > are you running inside an environment that changes your path? Drop a
    >
    > "import sys; print(sys.version)" at the top of your script and see
    >
    > what it's really running as.
    >
    >
    >
    > ChrisA
    Victor Hooi, Nov 4, 2013
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    6,932
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  2. Replies:
    2
    Views:
    5,907
  3. mikeyz9
    Replies:
    1
    Views:
    567
    Johannes Schaub (litb)
    Mar 11, 2010
  4. ctk70
    Replies:
    2
    Views:
    454
    ctk70
    Feb 10, 2005
  5. Replies:
    0
    Views:
    464
Loading...

Share This Page