Reputation: 3445
Here is my file structure that I am working with for my application. My problem is that I cannot get my test_ctd.py file to see my ctd.py file.
Here is my directory structure
FileParser
--Parsers
----ctd.py
--tests
----__init__.py
----test_ctd.py
--parse.py
I never used an init.py file and am struggling to understand it, but here is my attempt at adding ctd.py to my path.
import sys
import os.path
d = os.path.dirname(os.path.dirname(os.path.abspath('../../')))
from Parsers import ctd
Also I do not have any code in my parse.py file, but I will be using that to initiate the program. Would I need a init file for that as well so I can import the files from the Parsers folder?
Any help on how to access my files from within this program structure would be appreciated. Eventually it will be running on a web server, not sure if that makes a difference or not...
Thanks!
Upvotes: 1
Views: 143
Reputation: 85492
Move the __init__.py
file into Parsers
and add the directory FileParser
as absolute path to your PYTHONPATH. For example with sys.path.append('full/path/to/FileParser')
.
Upvotes: 1
Reputation: 17188
You need to make sure Python is actually looking in the right places. You can do this by modifying your PYTHONPATH
environment variable to include places where Python packages are found (such as this directory). You'll also need an __init__.py
file, to mark the directory as a Python package.
Or, the cheap, hacky way is by modifying sys.path
.
import sys
import os
sys.path.insert(0, os.path.join(os.path.abspath(os.path.dirname(__file__)), 'Parsers'))
import cdt
Upvotes: 1
Reputation: 474021
Parsers
and FileParser
must contain __init__.py
if you want to import something from ctd.py
. See Importing modules in Python and __init__.py.
Then, you can import ctd.py
from your tests scripts by doing relative imports like from ..Parsers import ctd
or by adding FileParser
to sys.path
and using from Parsers import ctd
.
Or, add the directory containing FileParser
to sys.path
and use from FileParser.Parsers import ctd
.
Hope that helps.
Upvotes: 1