Python 3: Good location(s) to install own and 3rd

2019-07-31 09:57发布

问题:

For Python 3.x:

I'd like advice on best directory in which to locate modules that I want to use across multiple projects (and any related factors). I know about the import statement and PYTHONPATH, and various ways to make any location "work", but I want to be in line with standard practice.

So:

  1. Should I be putting my modules in a package in "Pythonxx\Lib\site-packages"?

  2. Does the site-packages directory get special treatment, or is it no different than other possible locations for modules? I'm confused as to whether it has special ".pth" and/or __init__.py behavior.

  3. To what extent are conventions influenced by what distutils does, which doesn't seem to be really ready for Python 3... and does that mean that I should be attending to some other conventions going forward?

  4. What should I make of this: http://docs.python.org/py3k/install/index.html in which "How installation works" seems to claim (in the table) that 'site-packages' is not relevant to Windows, though I see an empty site-packages directory in my Windows Python 3.1 installation.

-- Thanks!

回答1:

  1. Yes -- but let your setup.py do it

  2. No

  3. Why do you think there is a problem?

  4. The docs need fixing. The default Python install directory is C:\PythonXY, not C:\Python. The default package install directory is C:\PythonXY\Lib\site-packages.



回答2:

You might wish to consider setting up a virtualenv, which is the standard way of bundling a set of modules for use across projects and platforms.



回答3:

Coincidentally, it's the anniversary of my asking this question. Though the respondents attempted to be helpful, I ended up investigating this issue in substantially more depth. That led to notes which I posted here: Python- Organization for common modules. Also some issue reports at python.org (same user name), and some revisions to their documentation. I hope that helps others who are similarly stumped.



回答4:

Per-user site-packages directory is another option. You can try it with:

python setup.py install --user

Installing to %APPDATA%\Python\Python32 is much more lightweight than creating a whole new virtualenv. This is what PyPM does by default.