# Error running script file with absolute path

**URL:** <https://discuss.python.org/t/error-running-script-file-with-absolute-path/14035>\
**Category:** Python Help\
**Created:** [March 3, 2022, 5:58am UTC](https://discuss.python.org/t/error-running-script-file-with-absolute-path/14035 "2022-03-03T05:58:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ljh](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@ljh](https://discuss.python.org/u/ljh)\
**Post date:** [March 3, 2022, 5:58am UTC](https://discuss.python.org/t/error-running-script-file-with-absolute-path/14035/1 "2022-03-03T05:58:44Z")

</div>

Hi forum,

I get errors when I try running script with absolute path like this:  
$ python /home/someone/someapp/myapp.py

But the following without path is fine:  
$ cd /home/someone/someapp/  
$ python myapp.py

The code uses logging with f-string and format().  
f-string and format() cause different errors.

Thanks

```auto
# test:

$ pwd
/Users/ljh/Documents/helloPy
$ ls
appdir	libdir	log
$ 
$ find . -print | sed -e 's;[^/]*/;| ____;g;s;____ |; |;g'
.
| ____ appdir
| | ____ myapp.py
| ____ log
| | ____ myapp.log
$ 
$

1. ok, with f-string # line 10
$ cd /Users/ljh/Documents/helloPy/appdir && python3 ./myapp.py

2. error, with f-string
$ python /Users/ljh/Documents/helloPy/appdir/myapp.py         
  File "/Users/ljh/Documents/helloPy/appdir/myapp.py", line 13
    logging.info(f'{ __name__ }')
                             ^
SyntaxError: invalid syntax
$ 

3. ok, with format() # line 11
$ cd /Users/ljh/Documents/helloPy/appdir && python3 ./myapp.py

4. error, with format(): logging to stdout, not to log file
$ python /Users/ljh/Documents/helloPy/appdir/myapp.py         
2022-03-03 13:29:37,669 INFO /Users/ljh/Documents/helloPy/appdir/myapp.py:14:logConfig: __main__
$

# myapp.py:

import sys
import logging
from logging.handlers import RotatingFileHandler

def logConfig():
    logging.basicConfig(
        # handlers=[RotatingFileHandler('../log/myapp.log', maxBytes=10*1024*1024, backupCount=10)],
        handlers=[RotatingFileHandler('/Users/ljh/Documents/helloPy/log/myapp.log', maxBytes=10*1024*1024, backupCount=10)],
        level=logging.DEBUG,
        format="%(asctime)s %(levelname)s %(pathname)s:%(lineno)d:%(funcName)s: %(message)s")
    # logging.info(f'{ __name__ }') # line 10, Error, f-string
    logging.info('{}'.format( __name__ )) # line 11, Error, .format()

def main():
    logConfig()

if __name__ == ' __main__':
    main()

```

---

<div class="post-metadata">

**Author:** ![tjol](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjol/32/3414_2.png) [@tjol](https://discuss.python.org/u/tjol)\
**Post date:** [March 3, 2022, 8:35am UTC](https://discuss.python.org/t/error-running-script-file-with-absolute-path/14035/2 "2022-03-03T08:35:59Z")

</div>

Thanks for copying and pasting the actual commands you entered, rather than just describing what you did.

It looks like you’re sometimes calling `python`, and sometimes calling `python3`.

`python` is probably Python 2.7, which is a very old version which is no longer supported and is missing many features. Make sure you only use `python3`, and you should be fine.

---

<div class="post-metadata">

**Author:** ![ljh](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@ljh](https://discuss.python.org/u/ljh)\
**Post date:** [March 3, 2022, 8:45am UTC](https://discuss.python.org/t/error-running-script-file-with-absolute-path/14035/3 "2022-03-03T08:45:57Z")

</div>

Thanks Thomas,

My computer does have python 2 and 3. When I use python3 according to your help, it works!

Thank you for your time and patient for pointing this out.

# not python v3

python /Users/ljh/Documents/helloPy/appdir/myapp.py File "/Users/ljh/Documents/helloPy/appdir/myapp.py", line 13 logging.info(f'{\_\_name\_\_}') ^ SyntaxError: invalid syntax

# ok, with python3

python3 /Users/ljh/Documents/helloPy/appdir/myapp.py

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [March 5, 2022, 5:20am UTC](https://discuss.python.org/t/error-running-script-file-with-absolute-path/14035/4 "2022-03-05T05:20:30Z")

</div>

What’s happening, at least in your indicated example, is that Python 2 does not support the modern f-string syntax, which naturally results in a `SyntaxError`. As you discovered, running it with Python 3 solves this issue.

To note, its generally considered a best practice to let `logging` itself handle the formatting, using printf-style `%` placeholders in the string, and passing the variable data as positional parameters. This avoids building the logging message string except when it is actually needed, which improves performance, reduces the potential for bugs and doesn’t raise any errors doing so unless the logging message is actually emitted.

---

<div class="post-metadata">

**Author:** ![ljh](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@ljh](https://discuss.python.org/u/ljh)\
**Post date:** [March 5, 2022, 7:22am UTC](https://discuss.python.org/t/error-running-script-file-with-absolute-path/14035/5 "2022-03-05T07:22:12Z")

</div>

Hi,

Why printf style improves performance? Programmer specifies type format in printf, so the Python interpreter can do less? The online tutorial (1) states that the printf style is old (obsolete?).

Mismatch format and value in printf in C causes undefined behavior. Python checks this kind of mismatch.

```
name = 'snake'
age = '3' # value is in str type, not int type
print('name: %s, age: %d' % (name, age)) # TypeError
print(f'name: {name}, age: {age}')

```

1: [7. Input and Output — Python 3.10.2 documentation](https://docs.python.org/3/tutorial/inputoutput.html#old-string-formatting)

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [March 5, 2022, 7:44am UTC](https://discuss.python.org/t/error-running-script-file-with-absolute-path/14035/6 "2022-03-05T07:44:18Z")

</div>

I’m not suggesting using the `%` _operator_ for formatting, but rather using `logging`’s own string interpolation (which still uses the old printf-style _placeholders_ for backward compatibility reasons, unless you use one of the [various workarounds](https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles)). See, e.g. the [basic logging tutorial](https://docs.python.org/3/howto/logging.html#logging-variable-data) for more information. As I explained above, this (among other things) improves performance by only performing the string interpolation if the logging message is actually fired (e.g. lots of DEBUG log messages in a tight loop don’t impose a significant performance penalty on your application).

> [@ljh](#):
>
> Mismatch format and value in printf in C causes undefined behavior. Python checks this kind of mismatch.

EDIT: At least in this case, there’s no need for using anything but `%s` or %r here, which work with any Python object; other types are only required for specialized type-specific formatting. So in general, you’ll only run into these sorts of issues if there’s an actual logic bug in your code, rather than simply a non-matching type that could reasonably be handled.

> [@ljh](#):
>
> 1: [7. Input and Output — Python 3.10.2 documentation](https://docs.python.org/3/tutorial/inputoutput.html#old-string-formatting)

FYI, there’s no need for this legacy style formatting reminiscent of old text-only mailing lists in Discuss; like on most modern online platforms, you can simply use a normal inline link with [Markdown](https://example.com), [BBCode](https://example.com) or HTML.

---

<div class="post-metadata">

**Author:** ![ljh](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@ljh](https://discuss.python.org/u/ljh)\
**Post date:** [March 5, 2022, 8:08am UTC](https://discuss.python.org/t/error-running-script-file-with-absolute-path/14035/7 "2022-03-05T08:08:32Z")

</div>

Thank you, sir,

I think I get it now.

You are trying to teach me using this [log variable data](https://docs.python.org/3/howto/logging.html#logging-variable-data) instead of the other three kind of style.

```
name = 'snake'
age = 3

logging.info('name: %s, age: %d', name, age) # log variable data

logging.info('name: %s, age: %d' % (name, age)) # printf-style
logging.info('name: {}, age: {}'.format(name, age)) # .format()
logging.info(f'name: {name}, age: {age}') # f-string
```

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [March 5, 2022, 4:58pm UTC](https://discuss.python.org/t/error-running-script-file-with-absolute-path/14035/8 "2022-03-05T16:58:15Z")

</div>

> [@ljh](#):
>
> You are trying to teach me using this [log variable data](https://docs.python.org/3/howto/logging.html#logging-variable-data) instead of the other three kind of style.

Yup, exactly. To be more specific, you’re using the logger’s internal string interpolation rather than pre-interpolating the string _then_ passing it to the logger, as the other methods all do. That way, the string only gets interpolated if the message is actually logged, rather than always, which is generally more efficient.
