Access modules from test file - python

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!

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.

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

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').

Related

How do I import a file that is in another directory?

-Booking
-components
-db.py
-scripts
-insert_data.py
How do I import db.py in insert_data.py?
(I'm using python 3)
from components import db
Should work as long as components has a (potentially empty) __init__.py in it
Put __init__.py inside components folder
Access it from scripts folder using from ..components import db
There are several options, and it helps to know how Python's modules and packages work (which is not always completely straightforward and intuitive):
What's the difference between a Python module and a Python package?
What is __init__.py for?
Importing files from different folder
Relative imports for the billionth time
How to import a module given the full path?
https://docs.python.org/3/reference/import.html (even the official documentation can be helpful at times :)
One way is to explicitly modify the module search path in the beginning of your script:
import sys, os
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'components'))
import db
A less hacky way uses relative import, as described in Importing from a relative path in Python and Execution of Python code with -m option or not.

How to import modules regardless of the directory

I would like to know if there is any way to be able to use the modules of a project, regardless of the module's path, if it is in one or more directories above the current script
since what I have tried to do is something like this but it doesn't work.
import os
r = os.path.realpath(file.py)
from r import functionR
When I investigated a little more, I found something that talks about adding the modules to the main path, but I don't know if that is correct?
I hope you can help me a little
added an example route i'm using
root
folder1
folder11
file1.py
folder2
folder22
file2.py
file_one.py
For example, try using the file_one.py module in files that are at lower levels, for example file1.py or file2.py
I hope you can help me
you can try adding the directory containing the modules of interest to the sys.path
import sys
sys.path.extend([put_directory_here])
or add the directory to the PATH environment variable
or add the folder containing the modules to a location that is already in the path i.e. site packages
this way python knows where to look for the modules and they will be accessible anywhere
there's also a ton of info about absolute/relative imports of modules here: Relative imports for the billionth time

importing modules in application, not package

I am writing a Python (Python3) application (not a package) and have some doubts about the correct directory structure. At the moment I have this:
myapp/
__init__.py
launch.py
core/
__init__.py
some_core_class.py
other_core_class.py
gui/
__init__.py
some_gui_class.py
other_gui_class.py
I want the application to be started with python launch.py from any place in my directory structure - of course with prepending the correct path to launch.py, e.g. python myapps/myapp/launch.py.
Inside my modules I use absolute imports, e.g. in some_core_class.py I write from myapp.core.other_core_class import OtherCoreClass. I use the same way in launch.py, e.g. from myapp.core.some_core_class import SomeCoreClass.
But then launching it for example directly from dir myapp by writing python launch.py results in ImportError: No module named 'myapp'. I found I could make it work by changing my import in launch.py to from core.some_core_class import SomeCoreClass but this does not seem to me as a correct absolute import and is inconsistent with imports in other files.
What is the best way to solve my issue? I would like to avoid adding myapp to PATH environment variable which would require manual edit by the user or an installer. How should I change my code or my imports to make the application launchable from anywhere? Is that even possible?
I have just found, I can solve it by prepending
import os
import sys
app_parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(app_parent_dir)
at the very beginning of my launch.py. But this is so ugly and feels like a hack. There must be a better way. Python should not be ugly. Awaiting for better answers...

Better approach to use script inside nested directory PYTHONPATH

Sorry for asking my own question 2nd time, but i am totally stuck in import file in python.
I have a directory structure below:
|--test/foo.py
|--library #This is my PYTHONPATH
|--|--script1.py
|--|--library_1
|--|--|--script2.py
|--|--library_2
|--|--library_3
I am accessing library/library_1/script2.py from test/foo.py.
Here i am confused about what is the better approach. Generally all library folders or utility functions should be added to pythonpath.
This is a folder structure i am maintaining to differentiate utility functions and test scripts.
I tried putting __init__.py in library and library1 & then imported like from library1 import script2, but getting error as No module named script.
I have tried appending that path to system path as well.
Working: if i add another pythonpath like path/to/library/libray_1/. So should i do this for all folders which are inside library folder to make it work ?
Here's what you need to do:
|--test/foo.py
|--library #This is my PYTHONPATH
|--__init__.py
|--|--script1.py
|--|--library_1
|--|--|--__init__.py
|--|--|--script2.py
|--|--library_2
|--|--|--__init__.py
|--|--library_3
|--|--|--__init__.py
And inside the first __init__.py below library you need to do:
import library1
import library2
import script
Then, if library is your python path, you can do this within test/foo.py with no errors:
import library
library.library1.bar()
library.script.foo()

Import Python Issues

I've been writing in python for a couple of months now and I've never found a through explanation of how import works. I downloaded this folder with subfolders with python files in them. I'm trying to use one of these files and I'm loosing my mind. How do you properlly import a folder with all the files in it?
Any help would be greatly appreciated.
As written in the python documentation on modules:
If you have a folder sound looking like that :
sound/ Top-level package
__init__.py Initialize the sound package
effects/ Subpackage for sound effects
__init__.py
echo.py
surround.py
reverse.py
To import all files of effects folder :
from sound.effects import *
Note that to be able to import module, they have to contain an __init__.py file.
First, check to see if the subdirectories have a file named __init__.py file in them. Python will not recognize directories that do not contain these files.
Then, you will have to manually change the PYTHONPATH, which you can find in sys.path. You can find a great example here.
Edit: I'm not 100% sure this is what you were asking for. If you want to import ALL the python files in a directory, you will have to import them one by one. For example, given a directory like so:
parent/
__init__.py
runner.py
example.py
language.py
you would have to type
from parent import runner, example, language
or
from parent import * # this will also import __init__
You have to create a __init__.py file in the directory to make it a package. In this file you import all the symbols from the underlying files.
See http://docs.python.org/tutorial/modules.html (especially part 6.4 Packages) for further notes on that.
The parent folder must be either in PYTHONPATH or the folder path indicated in a file with extension .pth situated in a location in your path, usually in site-packages.
Then your package and all folders inside it from which you have to import need to have a file named __init__. This file can be used for program initialization but as a starting point it can be an empty file.
For example my program folder, situated in C:\python26 has the estructure:
programas\
.....package1\
.........__init__.py
.........module1.py
.........subpackage1\
.............__init__.py
.............module2.py
.....package2\
.........__init__.py
.........module3
.....__init__.py
.....lonelyscript1.py
.....lonelyscript2.py
file site-packages\site.pth contains:
C:\Python26\programas

Categories