0

I want to create a globally accessible module with utility methods and classes. It will be used in various projects, so I don't want the module to be specific to any one project. Obviously I could copy the directory into each new project, but I'm wondering if there's a better way. What's the best practice for something like this?

Cam Hashemi
  • 157
  • 1
  • 7
  • why not import? – maininformer Jan 14 '19 at 15:23
  • Possible duplicate of [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – TheStrangeQuark Jan 14 '19 at 15:23
  • you could upload your module to pypi or another package repository and then just pull it from there. though from what you are talking about it sounds like what you want is to make it a submodule. look up git submodules. that is prolly your best bet – Jacobr365 Jan 14 '19 at 15:24
  • Probably not pythonic, but I created a module for importing and just placed it in a folder, and added the folder onto my PATH environment (because I'm lazy). The more pythonic way to do that however, is to properly create and install your own module under the proper Python lib path (e.g. `lib/site-packages`) – r.ook Jan 14 '19 at 15:26
  • A quick-n-dirty way is just to put/copy your module into the `Python/Lib/site-packages` directory. To be a little more formal, you could write a `setup.py` script for your module that did this. – martineau Jan 14 '19 at 15:36

1 Answers1

1

The normal way of handling this would be to use pip to install your package in each of the virtual environments you use. If you aren't yet using virtualenvs, then you can make a module or package available by installing it in your Python interpreter's site-packages directory.

There are a number of complexities you would have to battle with to make your package pip-installable, so you will be relieved to know that for many purposes it's enough simply to copy the module into site-packages.

If you aren't familiar enough with your Python installation to know where site-packages lives, the following snippet might help:

>>> import sys
>>> for p in sys.path:
...   print(p)
...

/usr/local/anaconda3/envs/general/lib/python36.zip
/usr/local/anaconda3/envs/general/lib/python3.6
/usr/local/anaconda3/envs/general/lib/python3.6/lib-dynload
/usr/local/anaconda3/envs/general/lib/python3.6/site-packages
/usr/local/anaconda3/envs/general/lib/python3.6/site-packages/aeosa
>>>
holdenweb
  • 33,305
  • 7
  • 57
  • 77