-2

This is my file structure.

/working dir
    __init__.py
    main.py
    /packages
        __init__.py
        snafu.py
        /subfolder1
            __init__.py
            foo.py
        /subfolder2
            __init__.py
            bar.py
        /many_more
            ...

If I run main.py it will try to import, from subfolder1.foo import something But foo.py will try to import subfolder2 which won't work because subfolder2 is not found.

It would be way too much work to go into every file and change every import statement to from packages.a_subfolder.whatever import something

I have gotten it to work by adding /packages to the sys.path, but I would prefer not to do this. Is there a way to fix this using __init__.py files?

Would adding import * to the /packages __init__.py file work?

The many_more/ folders are third party packages I downloaded, since i work on this on different computers instead of installing the packages on every computer I work on it just uses the one in the folder. For example: to use googledrive in your program you need about 10 different packages to get it to work.

Admiralmatt
  • 15
  • 1
  • 6
  • 1
    See https://docs.python.org/2/tutorial/modules.html#intra-package-references – jonrsharpe Jul 22 '15 at 14:10
  • @jonrsharpe That would work, but then i would need to add that to about 50 import statements. I would need to add that to all foo.py and bar.py import statements as well as everything in all the other packages. – Admiralmatt Jul 22 '15 at 14:18
  • Well why were they written like that in the first place? Have you rearranged/restructured your package (wa-hey!) recently? – jonrsharpe Jul 22 '15 at 14:18
  • I didn't write most of them i'm just using them, i updated the question to explain more. – Admiralmatt Jul 22 '15 at 14:23
  • Who did write them? Are they supposed to be used together, and if so why aren't they already arranged in a package? – jonrsharpe Jul 22 '15 at 14:24
  • Please read [mcve]. Without concrete examples there's no point discussing this further. if you've correctly installed the third-party packages there's no need to dump them all into your project - use a `requirements.txt` or `setup.py` to work across multiple machines effectively. This will also make subsequent distribution of your package much easier. – jonrsharpe Jul 22 '15 at 14:28
  • I updated the question, but the point was to avoid the need to install it every time i go to test the program. – Admiralmatt Jul 22 '15 at 14:40
  • If you write an appropriate `setup.py`, then `python setup.py test` will deal with it for you. You seem to be putting a lot of effort into avoiding the usual workflows - why? – jonrsharpe Jul 22 '15 at 14:42
  • So if i write the `setup.py` file, i can plug in my flash drive into the computer. open idle and run main.py. then i can remove my flash drive and all the packages will no longer be installed on the computer anymore? – Admiralmatt Jul 22 '15 at 14:49
  • No; why would you want that? Have you considered Portable Python or a `virtualenv`, if that's what you need? Have a look at e.g. http://stackoverflow.com/q/7798704/3001761 – jonrsharpe Jul 22 '15 at 14:51
  • regardless can it be done with `import *` in the **__init__** file or do i need to continue to add it to the sys.path? – Admiralmatt Jul 22 '15 at 14:53

2 Answers2

0

In your case it seems you want to import modules present in parent dierectory. Having following code in the file from which you want to import the module in the parent directory should work:

import sys
sys.path.append('.')
sys.path.append('..')
dsingh
  • 270
  • 1
  • 5
  • 17
0

So it looks like the only way that worked was to add

import sys, os
packagepath = os.getcwd() + '/packages'
sys.path.append(packagepath)

to the /packages __init__.py file and call it using from packages.subfolder1.foo import something

Admiralmatt
  • 15
  • 1
  • 6