importing python modules into another directory - python

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

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")

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 *

Importing another project as modules in 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.

Python 3 - ImportError: No module named

Situation:
Given this project structure:
project/
app/
__init__.py (empty)
stamp.py
tests/
test.py
main.py
In main.py and test.py I am trying to import the functionality of stamp.py via:
from app.stamp import Timestamp
Timestamp gets imported in main.py but not in test.py where I get this error:
ImportError: No module named 'app'
Question:
How can I in python 3.5 import functionality of stamp.py in test.py?
make sure your folder tests contains __init__.py
Below code appends the path of your project project to sys.path in test.py
python will go through to search the modules and files in your project
import sys
sys.path.append("/path/to/project")
from app.stamp import Timestamp
Make sure project/ is in your PYTHONPATH, put and __init__.py file in the project/ directory and then you will be able to call from project.app.stamp import Timestamp.

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