0

I have two python modules, one named testingDirectories.py and another named testFile.py

There are two folders in my directory: simulations and src

"simulations" is supposed to hold all the source code for individual simulations that are being run.

"src" holds the source code for the main project that I run the simulations on.

My testingDirectories.py file is in "simulations", while testFile.py is in "src".

The contents of testFile.py are as follows:

def testFunction(m):
    return 'hello, world'

The contents of testingDirectories.py are as follows:

import sys
import os

from src.testFile import testFunction

testFunction('hello')

When I run the simple demo, it gives me an error:

from src.testFile import testFunction
ImportError: cannot import name testFunction

The point of this demonstration is to figure out if the program can find the directory "src" without telling it to look in the root directory. It seems to be able to find other folders and their modules and functions, but not a simple test function like this one. Any ideas why I might be getting this error?

Blairg23
  • 11,334
  • 6
  • 72
  • 72
  • 1
    Probably relevant: http://stackoverflow.com/q/72852/3001761 – jonrsharpe Jun 20 '14 at 07:27
  • Add the directory to PATH env variable. That should solve. or you can keep testDirectory.py in the same level of "simulations" and "src" file then it it will be able to find the src. Or you can import any module from anywhere using "imp" module using imp.load_source – user2109788 Jun 20 '14 at 07:48

2 Answers2

0

You'll need a master script, placed in the same directory as simulations and src. From there, you can execute all the other scripts. Or just include src to simulations, and add some __init__ files.

Alternatively, you can make this structure:

|mypackage
    |__init__.py
    |simulations
        |__init__.py
        |testingDirectories.py
    |src
        |__init__.py
        |testFile.py

The __init__.py is fine being empty. Step two, include it in the PYTHONPATH.

$ set PYTHONPATH=%PYTHONPATH%;C:\my_python\mypackage

and then, in testingDirectories.py:

from mypackage.src.testFile import testFunction
testFunction('hello')

Another way? Execute the program with -m switch.

$ cd C:\my_python\mypackage\simulations\
$ python -m simulations.testingDirectories

And you can import the second module with just from src.testFile ...

Hope this helps!

aIKid
  • 26,968
  • 4
  • 39
  • 65
0

What I ended up doing (because apparently relative paths are a fairly common problem in Python):

I created a file named main.py that does a

os.walk('.')

to discover all files and folders and stores the module names in a dictionary for easy viewing.

I then gave main.py a few functions to allow the user to simply run my scripts from the commandline through main.py, rather than having to find each individual module and run it from there.

This is the code I used to run modules and functions using a user-defined string:

def runModuleFunction(moduleName, funcName, params):
    m = __import__(moduleName)
    func = getattr(m, funcName)
    return func(params)

This solution works perfectly, so far.

Blairg23
  • 11,334
  • 6
  • 72
  • 72