Let's say I have the following directory
model_folder
|
|
------- model_modules
| |
| ---- __init__.py
| |
| ---- foo.py
| |
| ---- bar.py
|
|
------- research
| |
| ----- training.ipynb
| |
| ----- eda.ipynb
|
|
------- main.py
and I want to import model_modules into a script in research
I can do that with the following
import sys
sys.path.append('/absolute/path/model_folder')
from model_modules.foo import Foo
from model_modules.bar import Bar
However, let's say I don't explicitly know the absolute path of the root, or perhaps just don't want to hardcode it as it may change locations. How could I get the absolute path of module_folder from anywhere in the directory so I could do something like this?
import sys
sys.path.append(root)
from model_modules.foo import Foo
from model_modules.bar import Bar
I referred to this question in which one of the answers recommends adding the following to the root directory, like so:
utils.py
from pathlib import Path
def get_project_root() -> Path:
return Path(__file__).parent.parent
model_folder
|
|
------- model_modules
| |
| ---- __init__.py
| |
| ---- foo.py
| |
| ---- bar.py
|
|
|
------- src
| |
| ---- utils.py
|
|
|
|
|
------- research
| |
| ----- training.ipynb
| |
| ----- eda.ipynb
|
|
------- main.py
But then when I try to import this into a script in a subdirectory, like training.ipynb, I get an error
from src.utils import get_project_root
root = get_project_root
ModuleNotFoundError: No module named 'src'
So my question is, how can I get the absolute path to the root directory from anywhere within the directory in python?
sys.path[0] contain your root directory (the directory where the program is located). You can use that to add your sub-directories.
import sys
sys.path.append( sys.path[0] + "/model_modules")
import foo
and for cases where foo.py may exist elsewhere:
import sys
sys.path.insert( 1, sys.path[0] + "/model_modules") # put near front of list
import foo
Good morning I am stuck with the following problem. Precisely, I have the following setup:
Project_Name
|
|--> __init__.py
|
|--> Tool1
| |
| |--> Object1.py
| |
| |--> __init__.py
|
|--> Tool2
|
|--> Object2.py
|
|--> __init__.py
where Project_name, Tool1 and Tool2 are folders. Object2 contains a class named 'House'. How can I use the class 'House' in Object1? I tried the following:
from Tool2.Object2 import House
but I receive error message 'No module named 'Tool2'.
What am I doing wrong? All init.py files are empty, should I change that?
1
If you are using VS code, the easiest fix is to start your script being executed like the following.
import sys
sys.path.append('/PATH/TO/Project_Name')
import Tool1.Object1
...
2
Or, you can add the environment variable PYTHONPATH to settings.json (can be found ctrl + shift + P then type settings.json)
"terminal.integrated.env.linux": {
"PYTHONPATH": "/PATH/TO/Project_Name/"
}
In this way, you can just
import Tool1.Object1
sys.path and PYTHONPATH will do the same for you.
You need to export PYTHONPATH to your Project_Name so that the interpreter knows the specific folder(s) to look up.
export PYTHONPATH=path/to/your/project
For example:
object1.py
from tool2.object2 import House
house = House()
house.print_message("Hello World!!!")
object2.py
class House(object):
def __init__(self):
pass
def print_message(self, text):
print(text)
Outputs before and after exporting PYTHONPATH
$ python tool1/object1.py
Traceback (most recent call last):
File "tool1/object1.py", line 2, in <module>
from tool2.object2 import House
ImportError: No module named tool2.object2
$ pwd
/Users/.../StackOverflow
$ export PYTHONPATH=/Users/.../StackOverflow
$ python tool1/object1.py
Hello World!!!
$ python object1.py
Hello World!!!
I am building Python bindings for a standalone C library that I wrote. The file layout of the library is as following:
<project root>
|
`- cpython
| |
| `- module1_mod.c
| `- module2_mod.c
| `- module3_mod.c
|
`- include
| |
| `- module1.h
| `- module2.h
| `- module3.h
|
`- src
| |
| `- module1.c
| `- module2.c
| `- module3.c
|
`- setup.py
I want to obtain a Python package so I can import modules in a namespace such as my_package.module1, my_package.module2, etc.
This is my setup.py so far:
from os import path
from setuptools import Extension, setup
ROOT_DIR = path.dirname(path.realpath(__file__))
MOD_DIR = path.join(ROOT_DIR, 'cpython')
SRC_DIR = path.join(ROOT_DIR, 'src')
INCL_DIR = path.join(ROOT_DIR, 'include')
EXT_DIR = path.join(ROOT_DIR, 'ext')
ext_libs = [
path.join(EXT_DIR, 'ext_lib1', 'lib.c'),
# [...]
]
setup(
name="my_package",
version="1.0a1",
ext_modules=[
Extension(
"my_package.module1",
[
path.join(SRC_DIR, 'module1.c',
path.join(MOD_DIR, 'module1_mod.c',
] + ext_libs,
include_dirs=[INCL_DIR],
libraries=['uuid', 'pthread'],
),
],
)
Importing mypackage.module1 works but the problem is that the external libraries are also needed by module2 and module3 (not all of them for all the modules), and I assume that if I include the same external libs in the other modules, I would get a lot of bloat.
I looked around sample setups in Github but haven't found an example resolving this problem.
What is a good way to organize my builds?
EDIT: This is actually a more severe problem in that I have symbols in module1 that are needed in module2, etc. E.g. an object in module2 requires an object type defined in module1. If I create separate binaries without including all sources for each dependency, the symbols won't be available at linking time, thus increasing redundancy and complexity of keeping track of what is needed for which module.
After a couple of days of digging into Python bug reports and scarcely documented features, I found an answer to this, which resolved both the multiple external dependencies and the internal cross-linking.
The solution was to create a monolithic "module" with all the modules defined inside it, then exposing them with a few lines of Python code in a package initialization file.
To do this I changed the module source files to header files, maintaining most of their methods static and only exposing the PyTypeObject structs and my object type structs so they can be used in other modules.
Then I moved the PyMODINIT_FUNC functions defining all the modules in a "package" module (py_mypackage.c), which also defines an empty module. the "package" module is defined as _my_package.
Finally I added some internal machinery to an __init__.py script that extracts the module symbols from the .so file and exposes them as modules of the package. This is documented in the Python docs :
import importlib.util
import sys
import _my_package
pkg_path = _my_package.__file__
def _load_module(mod_name, path):
spec = importlib.util.spec_from_file_location(mod_name, path)
module = importlib.util.module_from_spec(spec)
sys.modules[mod_name] = module
spec.loader.exec_module(module)
return module
for mod_name in ('module1', 'module2', 'module3'):
locals()[mod_name] = _load_module(mod_name, pkg_path)
The new layout is thus:
<project root>
|
`- cpython
| |
| `- my_package
| |
| `- __init__.py
|
| `- py_module1.h
| `- py_module2.h
| `- py_module3.h
| `- py_mypackage.c
|
`- include
| |
| `- module1.h
| `- module2.h
| `- module3.h
|
`- src
| |
| `- module1.c
| `- module2.c
| `- module3.c
|
`- setup.py
And setup.py:
setup(
name="my_package",
version="1.0a1",
package_dir={'my_package': path.join(CPYTHON_DIR, 'my_package')},
packages=['my_package'],
ext_modules=[
Extension(
"_my_package",
"<all .c files in cpython folder + ext library sources>",
libraries=[...],
),
],
)
For the curious, the complete code is at https://notabug.org/scossu/lsup_rdf/src/e08da1a83647454e98fdb72f7174ee99f9b8297c/cpython (pinned at the current commit).
I have the next directory structure:
|-Server/
|-------OrderBook/
| |--------message.py
| |--------orderBookObject.py
|-------Rabbit/
| |--------emisor.py
| |--------receptor.py
|-------server.py
|-------processMessage.py
In server.py I have "from processMessage import A"
In processMessage.py I have "from OrderBook.orderBookObject import B"
and in orderBookObject.py I have "from Rabbit.emisor import C"
but I have the next error "ModuleNotFoundError: No module named 'Rabbit"
Why is this happening?
How can I fix it?
Edit:
If I move Rabbit folder inside OrderBook folder, I have the same error.
create a file named __init__.py inside directory OrderBook and Rabbit
this will create package, and then you can import
https://docs.python.org/3/tutorial/modules.html#packages
so your directory structure will be looked like :
|-Server/
|-------OrderBook/
| |--------__init__.py
| |--------message.py
| |--------orderBookObject.py
|-------Rabbit/
| |--------__init__.py
| |--------emisor.py
| |--------receptor.py
|-------server.py
|-------processMessage.py
Assume I have projects deployment and cms with this structure:
+ deployment
| + src
| | + my_company
| | | + __init__.py
| | | + deployment
| | | | + ...
+ cms
| + src
| | + my_company
| | | + __init__.py
| | | + cms
| | | | + ...
+ ...
My company has many projects that are distributed as single logical package my_company. This functionality ensures extend_path in each my_company/__init__.py file.
https://docs.python.org/2/library/pkgutil.html#pkgutil.extend_path
So then is possible import like this:
from mp_company import cms
from mp_company import deployment
Problem comes when I mark all src directories as Sources Root in PyCharm. Because then PyCharm sees just only one package (probably the first it encounters) for the first level of imports in suggestions box. So if I want sugesstions for phrase import my_company. it appears only deployment. Strange is that for second level of imports all working right. So all suggestions for phrase import my_company.cms. suddenly appears after I write dot character after cms package name.
Is there any option in settings to fix this problem?
It looks like it is known issue https://youtrack.jetbrains.com/issue/PY-23087.