Categories
Python Answers

How to get monitor resolution in Python?

Sometimes, we want to get monitor resolution in Python.

In this article, we’ll look at how to get monitor resolution in Python.

How to get monitor resolution in Python?

To get monitor resolution in Python, we can use the screeninfo package.

To install it, we run

pip install screeninfo

Then we use it by writing

from screeninfo import get_monitors

for m in get_monitors():
    print(str(m))

to call get_monitors to get the data for all the monitors in the system.

And then we print the values as strings with print in the loop.

Conclusion

To get monitor resolution in Python, we can use the screeninfo package.

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.