1

My project hierarchy (Python 3.5):

/home/pavarine/Projects/brp   (project root)
   config.py
   main.py
   lib/
       Class1.py

I'm trying to dynamically transform my project (brp folder) into a python package and call it's modules from wherever I want, like this:

Execution from 'main.py':

sys.path.insert(0, "/home/pavarine/Projects/brp")
print('\n'.join(sys.path))

That gives me:

/home/pavarine/Projects/brp
/home/pavarine/Projects/brp
/usr/lib/python35.zip
/usr/lib/python3.5
/usr/lib/python3.5/plat-x86_64-linux-gnu
/usr/lib/python3.5/lib-dynload
/home/pavarine/.local/lib/python3.5/site-packages
/usr/local/lib/python3.5/dist-packages
/usr/lib/python3/dist-packages

Where I can clearly see thats my projects root path is now inside sys.path, but when I try to import the "config.py" I got an error:

Execution from 'main.py':

from brp import config

Results in:

ImportError: No module named 'brp'

What am I doindo wrong?

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
Pavarine
  • 637
  • 3
  • 15
  • 30

2 Answers2

3

You need to insert the parent directory into the python path.

However, please don't do this. Manipulating the python path from inside scripts is possibly dangerous and using an absolute path makes it unportable.

For your project, create another folder called brp and create a simple setup.py there. It should look like this

/home/pavarine/Projects/brp   (project root)
    setup.py
    brp/
       __init__.py # can be empty
       config.py
       main.py
       lib/
           __init__.py # can be empty
           Class1.py

For the start, setup.py can be as simple as

from setuptools import setup, find_packages

setup(
    name='brp',
    packages=find_packages(),
)

You can then install your package with pip install . from the root folder and use it anywhere on your system without manipulating the sys.path.

If you want, you can use pips developer mode: pip install -e ., this will just create symlinks so that changes in your project directory directly take effect without needing to reinstall the package.

MaxNoe
  • 14,470
  • 3
  • 41
  • 46
1

Usually you structure your project one 1 of two ways.

You have one module in your root folder

brp/
    brp.py

Or you have multiple modules which you put into one subfolder:

mymod/
    mymod/
        __init__.py
        a.py
        b.py

The simplest solution for you is to add a brp subfolder in the existing brp and move everything in that subfolder. Resulting in:

brp/
    brp/
        __init__.py
        config.py
        main.py
        lib/
             __init__.py
             Class1.py

The __init__.py files are so python knows that these are submodules. For more information there are several guides including this one.

Jacques Kvam
  • 2,856
  • 1
  • 26
  • 31