Importing another project as modules in python - python

Suppose I have a project in the following structure
projectfoo/
|- mymodule/
|--|- __init__.py
|--|- library.py
|- preprocessor.py
and in the __init__.py in mymodule looks like this
from . import library #library itself has other functions
def some_function():
blar blar blar...
and the preprocessor.py would look like follows
import mymodule
def main():
something()
def something():
mymodule.some_function() # calls the function defined in __init__.py
if __name__ == '__main__':
main()
Then I started projectbar, which is using a lot of common code from projectfoo. So instead of copying and pasting code between projects, I wish to import projectfoo into project bar, as follows.
projectbar/
|- projectfoo/
|--|- mymodule/
|--|--|- __init__.py
|--|--|- library.py
|--|- preprocessor.py
|- index.py
So I am trying to import preprocessor in my index.py as follows
from projectfoo import preprocessor
However I am getting an error saying preprocessor.py is now unable to import mymodule.
ImportError: No module named 'mymodule'
Am I doing this correctly? I am using python3.4 running in ubuntu 14.04 in my setup.
EDIT: I also tried adding __init__.py to projectfoo, but I am still getting the same error

You are getting this error because you did not added the path of preprocessor as a library package
from sys import path as pylib #im naming it as pylib so that we won't get confused between os.path and sys.path
import os
pylib += [os.path.abspath(r'/projectfoo')]
from projectfoo import preprocessor
FYI: os.path will return the absolute path. but sys.path will return the path env. variable in system settings.
Hope it helps.

Try to add empty __init__.py to projectfoo folder.

You have to add an __init__.py file (can be empty) in your projectfoo/ folder to make it a valid module.
Then use relative imports to explicitly specify you're requesting the current module's submodule mymodule like this:
from .projectfoo import preprocessor
The . stands for the current module in which the file containing the import statement is located. Its parent module would be denoted as .., its "grandparent" would be ... etc.

Related

Importing from parent directory python

I have the following structure:
app/
code/
script.py -- has a function called func
main.py
How can I import script.py from main.py ?
I tried from code.script import func and I got ModuleNotFoundError: No module named 'code.script'; 'code' is not a package
Place a __init__.py file in the code directory. This will allow your main.py code to import it as a module like you have tried there.
Indeed the best way is to add __init__.py in code directory because when a regular package is imported __init__.py file is implicitly executed and the objects it defines are bound to names in the package’s namespace.
FYI, as an alternative you can also to this in your main.py before the import:
import sys
sys.path.append("/path/to/script.py")

importing python modules into another directory

I have a ROS package that I am working with and I am trying to import a python module from another directory in the same package. My file structure is the follow:
package/
src/
__init__.py
lab03/
map_helper.py
__init__.py
lab04/
foo.py
__init__.py
I want to use helper.py in foo.py
foo.py
from src.lab03 import map_helper as helper
However I am getting the following error:
from src.lab03 import map_helper as helper ImportError: No module named src.lab03
You need to add package directory to your sys path to be able to import packages
import sys
sys.path.append('../../../package')
from src.lab03 import map_helper as helper
Have you tried this?
from package.src.lab03 import map_helper as helper

Best way to import a package located in a sibling directory

My file stucture is :
top\
my_package\
__init__.py
functions.py
scripts\
test.py
main.py
I would like to import the content of functions.py in test.py.
In main.py, I can import functions.py with from my_package.functions import ....
I was expecting to be able to import functions.py in test.py with something like from ..my_package.functions import ... but it raises the following error :
SystemError: Parent module '' not loaded, cannot perform relative import
top directory shoudln't be a package because I want to be able to run main.py without being running a script in a package.
What is the proper/pythonic way import functions.py from test.py ?
I could add my_package to the PYTHONPATH, but my code would be less portable. I'm using Python 3.5
This is happening because as far as python is concerned my_package and scripts directories are not related to each other.
To solve your problem you can add __init__.py to your top directory and try using
from ..function import *

How do you use __init__.py?

I'm trying to learn how the __init__.py file works for packaging and calling modules from different directories.
I have a directory structure like this:
init_test\
__init__.py
a\
aaa.py
b\
bbb.py
in aaa.py there is a function called test
bbb.py looks like this:
import init_test.a.aaa
if __name__ == "__main__":
init_test.a.aaa.test()
but this gives me ImportError: No module named a.aaa
What am I doing wrong? I've tried doing the same basic thing from a module above the package structure as opposed to inside the package and that did not work either? My __init__.py
You also need to have __init__.py in a and b directories
For your example to work first you should add your base directory to the path:
import sys
sys.path.append('../..')
import init_test.a.aaa
...
You have to add an empty __init__.py into a. Then a is recognized as a sub package of init_test and can be imported. See http://docs.python.org/tutorial/modules.html#packages
Then change import init_test.a.aaa to import ..a.aaa and it should work. This is -- as Achim says -- a relative import, see http://docs.python.org/whatsnew/2.5.html#pep-328
If you really want to run bbb.py, you have to put init_test/ on your python path, e.g.
import sys
import os
dirname = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(dirname, "../.."))
import sys
sys.path.insert(0, ".")
import init_test.a.aaa
if __name__ == "__main__":
inittest.a.aaa.test()
And then you can start
python init_test/b/bbb.y
or if you are inside b/
python bbb.py
__init__.py needs to be in all folders that you want to use as modules. In your case, this means init_test/a and init_test/b too.

How can I import a C++ python extension into a module in another directory?

Here is the directory structure:
app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
sub2.so
test_sub2.py
The folder app is on my PYTHONPATH
All of the __init__.py files are empty.
The shared library sub2.so is a C++ extension module that I compiled using cmake and boost-python.
test_sub2.py is a test script for the class defined in sub2.so.
If I run test_sub2.py from the sub2 directory, it imports the module correctly and the test passes.
How do I import the class A from sub2.so into mod1.py?
The way to import it is to import app.sub2.sub2, from any source file. Your test should actually live outside of app and use that module-path to get to the extension module.
Try
import .app.sub2.sub2
in your mod1.py file
Use relative imports:
from ..sub2.sub2 import A
This is similar to a relative path "../sub2/sub2.so".

Categories

Resources