Categories
Python Answers

How to determine application path in a Python EXE generated by pyInstaller?

Sometimes, we want to determine application path in a Python EXE generated by pyInstaller.

In this article, we’ll look at how to determine application path in a Python EXE generated by pyInstaller.

How to determine application path in a Python EXE generated by pyInstaller?

To determine application path in a Python EXE generated by pyInstaller, we can check the sys.frozen property.

For instance, we write

import os
import sys

config_name = 'myapp.cfg'

if getattr(sys, 'frozen', False):
    application_path = os.path.dirname(sys.executable)
elif __file__:
    application_path = os.path.dirname(__file__)

config_path = os.path.join(application_path, config_name)

to get the value of sys.frozen with

getattr(sys, 'frozen', False)

If the value is True, then the script code is running in the exe file.

Then we get the path of the exe it’s running on with

os.path.dirname(sys.executable)

Otherwise, the script it running as a script and we get the path of the script with

os.path.dirname(__file__)

Conclusion

To determine application path in a Python EXE generated by pyInstaller, we can check the sys.frozen property.

Categories
Python Answers

How to format a decimal to always show 2 decimal places with Python?

Sometimes, we want to format a decimal to always show 2 decimal places with Python.

In this article, we’ll look at how to format a decimal to always show 2 decimal places with Python.

How to format a decimal to always show 2 decimal places with Python?

To format a decimal to always show 2 decimal places with Python, we can use the string format method.

For instance, we write

from math import pi

s = '{0:.2f}'.format(pi)

to call '{0:.2f}'.format with pi to return a string that has pi in rounded to 2 decimal places.

Conclusion

To format a decimal to always show 2 decimal places with Python, we can use the string format method.

Categories
Python Answers

How to install a module using pip for specific Python version?

Sometimes, we want to install a module using pip for specific Python version.

In this article, we’ll look at how to install a module using pip for specific Python version.

How to install a module using pip for specific Python version?

To install a module using pip for specific Python version, we can use the version number as a flag when running python.

For instance, we write

py -2 -m pip install pyfora

to use py -2 to run pip to install packages for Python 2.

Conclusion

To install a module using pip for specific Python version, we can use the version number as a flag when running python.

Categories
Python Answers

How to add N seconds to datetime.time in Python?

Sometimes, we want to add N seconds to datetime.time in Python.

In this article, we’ll look at how to add N seconds to datetime.time in Python.

How to add N seconds to datetime.time in Python?

To add N seconds to datetime.time in Python, we can create a timedelta object and add that to the time.

For instance, we write

import datetime

a = datetime.datetime(100, 1, 1, 11, 34 ,59)
b = a + datetime.timedelta(0, 3)
print(a.time())
print(b.time())

to create a datetime with

a = datetime.datetime(100, 1, 1, 11, 34 ,59)

Then we add a timedelta object to it to add 3 seconds to the datetime with

b = a + datetime.timedelta(0, 3)

The first argument of timedelta is the number of days to add.

Conclusion

To add N seconds to datetime.time in Python, we can create a timedelta object and add that to the time.

Categories
Python Answers

How to replace non-ASCII characters with a single space with Python?

Sometimes, we want to replace non-ASCII characters with a single space with Python.

In this article, we’ll look at how to replace non-ASCII characters with a single space with Python.

How to replace non-ASCII characters with a single space with Python?

To replace non-ASCII characters with a single space with Python, we can use string’s join method with list comprehension.

For instance, we write

''.join([i if ord(i) < 128 else ' ' for i in text])

to replace all non-ASCII characters with spaces with

i if ord(i) < 128 else ' '

Is the character code is less than 128 as returned by ord then the character is an ASCII character.

i is the character being iterated through in the text string.

We put the converted characters in a list and then call join on the list to join the list back into a string.

Conclusion

To replace non-ASCII characters with a single space with Python, we can use string’s join method with list comprehension.