-1

G:\code\Python\hello\helloworld.py

G:\code\Python\hello\mymodule\gettime.py

G:\code\Python\hello\mypath.pth

I write gettime.py in mymodule, I'd import gettime in helloworld. I write "G:\\code\\Python\\hello\\mymodule" in mypath.pth. Can I put mypath.pth in the directory "G:\code\Python\hello"?

Community
  • 1
  • 1
D.L
  • 3
  • 1
  • 3

1 Answers1

1

No, it won't work, per the site documentation:

A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above.

The four directories mentioned "above" are specific to the Python install, they're not related to the working directory. It's actually more than four (they're not including the optional user site-packages directory for instance), but the list is not arbitrarily long, and the working directory is never involved in determining what .pth files to use.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • You _could_ implement a `sitecustomize` module which implements something like this (or make a package which installs a `.pth` file into site-packages which implements something like this) -- sounds insane but I've seen a (closed source) package which bootstraps a dev environment that does just this. – anthony sottile Sep 07 '17 at 04:50
  • @AnthonySottile: Yeah, if you're at that point, you could do it, but it's not just a "drop .pth in working directory, stuff works" solution. It's a "use hooks to execute arbitrary code" solution, and answering with "arbitrary code can do whatever you want" feels like cheating. :-) – ShadowRanger Sep 07 '17 at 05:30
  • The `site` documentation gives you a way to do this with [`site.addsitedir`](https://docs.python.org/3/library/site.html#site.addsitedir). – anthony sottile Sep 07 '17 at 15:24
  • @AnthonySottile: Yar. Though again, you'd have to gain execution in some way before you could do it. Normally, if you're in such a position, the better solution is to install the package to a normal location (global or user site-packages, or a virtualenv); if you're hoping to have `.pth` files automatically processed, it sounds more like a hack to avoid installation, when the real answer is "just install it!". – ShadowRanger Sep 08 '17 at 18:12
  • I totally agree that it's a -bad- **terrible** idea :) just wanted to dispute that it's impossible. – anthony sottile Sep 08 '17 at 18:54