简体   繁体   中英

Should I add my Python project to the site-packages directory, or append my project to PYTHONPATH?

I have a Python project which contains three components: main executable scripts, modules which those scripts rely on, and data (sqlite3 databases, flat files, etc.) which those scripts manipulate. The top level has an __init__.py file so that other programs can also borrow from the modules if needed.

The question is, is it more "Pythonic" or "correct" to move my project into the default site-packages directory, or to modify PYTHONPATH to include one directory above my project (so that the project can be imported from)? On the one hand, what I've described is not strictly a "package", but a "project" with data that can be treated as a package. So I'm leaning in the direction of modifying PYTHONPATH (after all, PYTHONPATH must exist for a reason, right?)

Definitely do not add your project to site-packages this is going to spoil your system Python installation and will fire back at the moment some other app would come there or you would need to install something.

There are at last two popular options for installing python apps in isolated manner

Using virtualenv

See virtualenv project. It allows

  • creation of new isolating python environment - python for this environment is different from system one and has it's own PYTHONPATH setup this allows to keep all installed packages private for it.
  • activation and deactivation of given virtualenv from command line for command line usage. After activate , you can run pip install etc. and it will affect only given virtualenv install.
  • calling any Python script by starting by virtualenv Python copy - this will use related virtualenv (note, that there is no need to call any activate )

using zc.buildout

This package provides command buildout . With this, you can use special configuration file and this allows creation of local python environment with all packages and scripts.

Conclusions

  • virtualenv seems more popular today and I find it much easier to learn and use
  • zc.buildout might be also working for you, but be prepared for a bit longer learning time
  • installing into system Python directories shall be always reserved for very special cases (pip, easy_install), better avoid it.
  • installing into private directories and manipulatig PYTHONPATH is also an option, but you would repeat, what virtualenv already provides

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM