I have a module with __init__.py located in a specific path. Then I:
import sys
sys.path.append('C:/Users/ME/Dropbox/Python/Library/ModuleDir')
But when I from ModuleDir.x import classA, it doesn't recognize the statement meaning that it wasn't able to find the module. What am I missing?
Under ModuleDir:
ModuleDir
__init__.py
X.py
Subdir
__init__.py
y.py
You need to add the directory containing the package subdir to your path:
sys.path.append('C:/Users/ME/Dropbox/Python/Library')
See docs.
Try sys.path.insert()
import sys
sys.path.insert(1, 'C:/Users/ME/Dropbox/Python/Library/')
from ModuleDir.x import classA
Related
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
I am using Python 3.4
I have a directory structure that looks this:
A
B
c.py
d.py
__init__.py
C
e.py
f.py
__init__.py
g.py
__init__.py
From g.py I can import things from both B and C modules.
I need, in e.py, to import something from c.py
I tried:
import B
and
from B.c import stuff_I_need
For both I get the error:
"No module named B".
I also tried something like:
from A.B.c import stuff_I_need
I am further confused by the fact with an identical directory structure, I can make the imports I need with Python 2.7.
Can you help me figure out what's going on?
Solution:
PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
Taken from here.
When importing it looks at the the python folder for imports and the files in the local directory. If you want to import a file that is in neither of those, then I suggest using the sys module
import sys
sys.path.append(r'file-path\A') # Folder A
import B.c
If you don't want to set the full file path then you can also just backtrack to the previous directory with this for the same effect.
sys.path.append('..') # Previous Directory
You need to do either
from .B import c
or
import A.B.c
Reference:
What's New in Python 2.5
PEP 328 - Imports: Multi-Line and Absolute/Relative
I have several python modules in the project and I put them in different folders, for example,
pythonProject\folderA\modulex.py
pythonProject\folderB\moduley.py
pythonProject\commonModule\module1.py
I have __init__.py in each folder.
In this situation, how can I import module1 into modulex?
Use relatively import
# in modulex
from ..commonModule import module1
Whenever you have python packages (those folders that contain __init__.py files), you can import the modules like below
modulex.py
----------
from pythonproject.commonModule import module1
Try this, If the pythonproject is not defined by the tool, then you could use the relative addressing like below
from ..commonModule import module1
The best if all modules are in the same directory. In case any of them in different possible use of os.chdir(path). With os.chdir(path) method (https://docs.python.org/3.2/library/os.html) possible change working directory in your program.
import os
import modulex
#assume working directory is "pythonProject\folderA\"
os.chdir(r'pythonProject\commonModule\')
#now working directory is "pythonProject\commonModule\"
import module1
I have a Python package with several modules.
What I can do:
It's possible to use all modules from the package in my program by modifying __init__.py of the package, by importing all modules in it:
#__init__.py
import module1
import module2
Then I simply import package in my program and can access classes/functions in all the modules by their full name
#My program
import package
a = package.module1.A()
QUESTION:
Is there any way to automate addition of imports to __init__.py so I don't need to specify them manually?
This is another answer that might be closer to what you want.
In __init__.py you can add this to import all python files in the package.
Note that it doesn't do packages.. not sure how. And I'm using windows
from os import listdir
from os.path import abspath, dirname, isfile, join
# get location of __init__.py
init_path = abspath(__file__)
# get folder name of __init__.py
init_dir = dirname(init_path)
# get all python files
py_files = [file_name.replace(".py", "") for file_name in listdir(init_dir) \
if isfile(join(init_dir, file_name)) and ".py" in file_name and not ".pyc" in file_name]
# remove this __init__ file from the list
py_files.remove("__init__")
__all__ = py_files
The init file doesn't work like that.. I think you're thinking of something like this..
if you have the file structure:
my_program.py
/my_package
__init__.py
module1.py
module2.py
in __init__.py you can write this line to edit the way import * works for my_package
__all__ = ["module1", "module2"]
now in my_program.py you can do this:
from my_package import *
a = module1.A()
Hope that helps!
You can read more here: https://docs.python.org/2/tutorial/modules.html#importing-from-a-package
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.