0

I'm currently working on a project that has a custom designed Python package, along with a bunch of scripts that use that package. What's the best way of structuring this such that I can run the scripts from anywhere without getting package not found errors? I'd like to build tests for the package as well, so I was thinking of having something like:

project/
|--src
|   |--some_package
|--test
|--scripts

But then I'm not sure how to have the scripts import my custom package such that I can run/reference the scripts from anywhere without "package can't be found" errors. Any help is appreciated!

2 Answers 2

2

There is documentation for this at the Hitch Hikers Guide to Packaging

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

Comments

0

One way to install and run these scripts on other machines is to package them up using distutils. See http://docs.python.org/library/distutils.html.

Otherwise, if you put an __init__.py file into a directory on your tree, Python will see the directory as a package and will allow you to import modules from it. For example, if you have this structure:

project/
|some_script.py
    |--some_package 
        |__init__.py
        |some_module.py
|--test
    |__init__.py
|--scripts
    |__init__.py

in some_script.py you could do this:

import some_package.some_module

That will allow you to do imports from subdirectories without an elaborate installation to put your modules somewhere in the Python path. The same thing can be done for the 'test' and 'scripts' directories. (You probably already know this, but __init__.py can just be an empty file.)

3 Comments

Yeah, but that's ugly, and I really don't want to have to do that at the top of every script. On top of that, I'm pretty sure this requires absolute paths, in which case my path will be different from someone else's who uses the script, and thus will not be scalable.
Well, your question didn't say you wanted to install it on other machines. In that case, you'll need docs.python.org/library/distutils.html .
I edited the answer to show a different approach without mucking with the Python path. I don't know, you may think this is ugly too.

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.