6

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)

2
  • You can make a package and place it somewhere within PYTHONPATH. Commented May 16, 2016 at 3:59
  • 2
    Your assumption about pip is not true. It supports private package sources, for example git repositories. Commented May 16, 2016 at 4:05

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

2 Comments

thanks @Gerasimov, this is great to know. I guess though it won't be available on command line?
@user308827 you can try to add utils path to PYTHONPATH in command line, see: stackoverflow.com/a/4580120/1113207

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.