Python Importing Module Error, "Import Error: cannot import name X" -



Python Importing Module Error, "Import Error: cannot import name X" -

i have 2 python modules, 1 named testingdirectories.py , named testfile.py

there 2 folders in directory: simulations , src

"simulations" supposed hold source code individual simulations beingness run.

"src" holds source code main project run simulations on.

my testingdirectories.py file in "simulations", while testfile.py in "src".

the contents of testfile.py follows:

def testfunction(m): homecoming 'hello, world'

the contents of testingdirectories.py follows:

import sys import os src.testfile import testfunction testfunction('hello')

when run simple demo, gives me error:

from src.testfile import testfunction importerror: cannot import name testfunction

the point of demonstration figure out if programme can find directory "src" without telling in root directory. seems able find other folders , modules , functions, not simple test function one. ideas why might getting error?

you'll need master script, placed in same directory simulations , src. there, can execute other scripts. or include src simulations, , add together __init__ files.

alternatively, can create structure:

|mypackage |__init__.py |simulations |__init__.py |testingdirectories.py |src |__init__.py |testfile.py

the __init__.py fine beingness empty. step two, include in pythonpath.

$ set pythonpath=%pythonpath%;c:\my_python\mypackage

and then, in testingdirectories.py:

from mypackage.src.testfile import testfunction testfunction('hello')

another way? execute programme -m switch.

$ cd c:\my_python\mypackage\simulations\ $ python -m simulations.testingdirectories

and can import sec module from src.testfile ...

hope helps!

python module path directory

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -