5

I have multiple python projects and each of them has a utility file, all of them with the same functions.

How can I set up some sort of local python library which I can import to all my local projects? I know rodeo has some functionality that you can use to designate some functions/files to be available across multiple python projects. Is there a way to do that outside of rodeo?

I do not want to create a library that is available through pip (since that would mean exposing it to everyone)

user308827
  • 18,706
  • 61
  • 194
  • 336

2 Answers2

3

You can create a local python library by creating a python package. It is as easy as putting a file named: __init__.py, into your utils lib.

You can read here more about the concept of creating a python packages

And here about how to write a good __init__.py

To finish, I just need to mention the PYTHONPATH, and an almost similar question about importing from a parent directory

Hope it was helpful for you.

Kings85
  • 315
  • 1
  • 14
  • I think the formatting disguised the correct file name. You meant, `__init__.py`. I would edit your post, but it won't let me with just those few edits. – Nick May 16 '16 at 04:42
3

If you're using PyCharm, you can add directory with local utils to project as "content root":

File -> Settings -> Project -> Project Structure -> Add content root

After that your utils would be available in project even if they're placed somewhere else.

If you're not using PyCharm you would need somehow add your external files to project's path. For example, dynamically.

Community
  • 1
  • 1
Mikhail Gerasimov
  • 27,590
  • 10
  • 86
  • 127