Why the import .module syntax

In pytorch/__init__.py at master · pytorch/pytorch · GitHub there’s a line where you try to get classes from a file called writer.py with the syntax “from .writer import…”

Why on earth is the . there? I had to remove the dot, and the dot prefix from many other files in the same directory, in order to get things to work.

Side note: Even then, the use of LooseVersion raised a deprecation error, which I had to deal with by using packaging.version.version from setuptools._vendor instead.

The leading dot indicates a relative import as described here:

Relative imports use leading dots. A single leading dot indicates a relative import, starting with the current package. Two or more leading dots indicate a relative import to the parent(s) of the current package, one level per dot after the first.

I don’t know what your use case is, but you should not change the source files and also not use the PyTorch source code as your working directory as it could raise import errors.

1 Like

Thanks for the reply!

Yeah, I figured out the . meant a relative import. It seems like I had another package called tensorboard installed closer than the package I should have been using. That was the cause of those errors.

Also, thanks for the advice about not using the source code as my CWD. Funnily enough, the problem was the opposite of what you advised. I got more import errors when I wasn’t calling code from the within the source code directory. Still, I got it fixed and managed to resolve that error so I could run the code in the “Try it out” section of GitHub - joonleesky/train-procgen-pytorch: Pytorch implementation on OpenAI's Procgen ppo-baseline, built from scratch. without getting import errors (code still crashes though…)

But I don’t know what to say about changing the source files. Like, the code raises a warning to use classes from packaging.version instead of LooseVersion. Just ignoring it seems like a bad idea, no? If you look at the source for LooseVersion, the author claims they’ve given up on getting the code working right, so that made using it seem like an even worse idea.