Categories
Python Answers

How to state in requirements.txt a direct GitHub source with Python?

Sometimes, we want to state in requirements.txt a direct GitHub source with Python.

In this article, we’ll look at how to state in requirements.txt a direct GitHub source with Python.

How to state in requirements.txt a direct GitHub source with Python?

To state in requirements.txt a direct GitHub source with Python, we can add the GitHub repo path into requirements.txt.

For instance, we can specify the commit hash by writing

package-one==1.9.4
git+https://github.com/path/to/package-two@41b95ec#egg=package-two
package-three==1.0.1

in requiements.txt.

We specify the GitHub path in the 2nd line.

Also, we can specify the branch with

git+https://github.com/path/to/package-two@master#egg=package-two

We can specify the release with

git+https://github.com/path/to/package-two@0.1#egg=package-two

And we can specify the release with

git+https://github.com/path/to/package-two@releases/tag/v3.7.1#egg=package-two

Conclusion

To state in requirements.txt a direct GitHub source with Python, we can add the GitHub repo path into requirements.txt.

Categories
Python Answers

How to run mkdir -p functionality in Python?

Sometimes, we want to run mkdir -p functionality in Python.

in this article, we’ll look at how to run mkdir -p functionality in Python.

How to run mkdir -p functionality in Python?

To run mkdir -p functionality in Python, w ecan use the pathlib‘s mkdir method.

For instance, we write

import pathlib

pathlib.Path("/tmp/path/to/desired/directory").mkdir(parents=True, exist_ok=True)

to create a path object with

pathlib.Path("/tmp/path/to/desired/directory")

Then we call mkdir with parents and exist_ok both set to True to create the directory at /tmp/path/to/desired/directory regardless of whether the parent directory exist or not.

Conclusion

To run mkdir -p functionality in Python, w ecan use the pathlib‘s mkdir method.

Categories
Python Answers

How to insert line at middle of file with Python?

Sometimes, we want to insert line at middle of file with Python.

In this article, we’ll look at how to insert line at middle of file with Python.

How to insert line at middle of file with Python?

To insert line at middle of file with Python, we can use the readlines and insert methods.

For instance, we write

with open("path_to_file", "r") as f:
    contents = f.readlines()

contents.insert(index, value)

with open("path_to_file", "w") as f:
    contents = "".join(contents)
    f.write(contents)

to open the file by calling open with the path and the permission string.

Then we call f.readlines to read the lines in the file.

Next, we call content.insert with the line index to write the line into and the value of the line.

And then we call open again to open the same file with write permission.

We have "".join(contents) to join the lines into one string.

Then we call f.write with contents to write the file contents.

Conclusion

To insert line at middle of file with Python, we can use the readlines and insert methods.

Categories
Python Answers

How to change directory with Subprocess in Python?

Sometimes, we want to change directory with Subprocess in Python.

In this article, we’ll look at how to change directory with Subprocess in Python.

How to change directory with Subprocess in Python?

To change directory with Subprocess in Python, we use the os.chdir method.

For instance, we write

import os

wd = os.getcwd()
os.chdir("/")
subprocess.Popen("ls")
os.chdir(wd)

to call os.chir to change the directory to /.

Then we call Popen with 'ls' to run the ls command on /.

Then we go back to the current working directory with

os.chdir(wd)

where wd is the current working directory path that we get with getcwd.

Conclusion

To change directory with Subprocess in Python, we use the os.chdir method.

Categories
Python Answers

How to display the time in a different time zone with Python?

Sometimes, we want to display the time in a different time zone with Python.

In this article, we’ll look at how to display the time in a different time zone with Python.

How to display the time in a different time zone with Python?

To display the time in a different time zone with Python, we can use the ZoneInfo class.

For instance, w write

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

now_pacific = datetime.now(ZoneInfo('US/Pacific'))
print(now_pacific.isoformat(timespec='seconds'))

to create the datetime with the current date and time at the US/Pacific time zone with

now_pacific = datetime.now(ZoneInfo('US/Pacific'))

Then we use

now_pacific.isoformat(timespec='seconds')

to return the date time string in ISO 8601 (YYYY-MM-DD) format.

Conclusion

To display the time in a different time zone with Python, we can use the ZoneInfo class.