My app structure is the following:
./mod1/__init__.py
./mod1/utils.py
./mod2/__init__.py
./mod2/test.py
Now in ./mod2/test.py I do:
from mod1 import utils
But I get an ImportError that no module is named utils. What's wrong?!
In order for this to work, the parent directory of mod1 and mod2 must be in sys.path, which probably means that it needs to be in the environment variable PYTHONPATH. Please see the module search path documentation.
One solution that does not require modification of PYTHONPATH is to place your executable script in the parent directory of mod1 and mod2.
Add a top-level folder in the sys.path:
import sys
sys.path.append('path_to_app_folder')
You should write this line before from mod1 import utils. Also add __init__.py:
./mod1/__init__.py
./mod1/utils.py
./mod2/__init__.py
./mod2/test.py
__init__.py
You can dynamically get the path to the app folder from ./mod2/test.py, using os.path.abspath. So, you code for ./mod2/test.py will look like this:
import os
import sys
top_level_folder = os.path.abspath('../')
sys.path.append(top_level_folder)
from mod1 import utils
Related
I've the following hierarchy:
.root
/program/main.py
/functions/myfunctions.py
And using my main.py I want to use the functions present in myfunctions.py script. For that I've defined my PYTHONPATH AS ./root/functions and I have this as the import on my script:
from functions import myfunctions as func
But I'm getting this error:
ModuleNotFoundError: No module named 'functions'
How can I solve this?
Thanks
You defined your PYTHONPATH as ./root/functions, so everything(modules/packages) "inside" that directory is recognizable by Python. ./root/functions directory is gonna get inserted to the sys.path. These paths are where Python loader checks to find modules and packages.
Just import the myfunctions.py:
import myfunctions as func
If you had defined PYTHONPATH as ./root, then you would have access to functions directory. (It became your namespace package for more information)
Import builtin module pathlib
Add the following code in your myfunctions.py
from pathlib import Path
Path(__file__).resolve().parent.parent
I have a module in my PYTHONPATH that looks like this:
module.py
from __future__ import absolute_import
import foo
foo.do_something()
print("I am a module!")
I later call this script using python -m module. The script works most of the time but I found that I'm cd'ed into a directory which has a folder with foo/__init__.py inside of it, Python prefers that folder over the other foo module in my PYTHONPATH. I assume it's because Python is treating the foo folder as an implicit, relative import (I'm using Python 2.7).
How do I get Python to not treat the folder in the $PWD as an implicit relative import?
from __future__ import absolute_import does not work
module.py and every other module I use has from __future__ import absolute_import included. So existing questions such as Python: Disabling relative import do not solve this issue.
You can remove the current directory from sys.path, or put the directory of the foo module at the top of sys.path
# at the very beginning of module.py
import sys
sys.path = sys.path[1:]
# or
sys.path.insert(0, <directory of foo module>)
# your code ...
from __future__ import absolute_import
import foo
foo.do_something()
print("I am a module!")
I'm using one package that with __init__.py import only one variable from module, but whole module itself is not exposed. Is there a way to access that module?
Lets look in this case:
Whole package:
test_package/
├── __init__.py
└── test_me.py
Now contents:
__init__.py:
from .test_me import test_me
test_me.py:
STATIC = 'static'
class Test:
pass
test_me = Test()
Now if I import package test_package. I can only access variable test_me, which is an instance of Test class. Though I can't access STATIC variable, because module itself was not exposed.
Is there a way to access test_me module in this case and not only one of its variables?
P.S. If I use sys to append path directly to that package's module, it throws error that such module does not exist when I try to import it.
If you add the package directory to your path, Python can import any file in that directory as if it were a module by itself.
import sys
sys.path.extend(test_package.__path__)
import test_me
print(test_me.STATIC)
You need to import them through __init__.py, so change its contents to:
from .test_me import test_me, STATIC
Now the following will work:
import test_package
print(test_package.STATIC)
Let's assume I have the following structure.
main.py
/mod1
__init__.py
mod1.py
/mod2
__init__.py
mod2.py
And I have the following line in main.py.
import mod1.mod2
In this case does mod1 also get imported?
Yes; mod1 is imported as well, and you can access mod1 solely as mod1 within your code if you do not write an alias like this import mod1.mod2 as mod2.
Python needs to import the modules consecutively so that it is able to import last module. You can test this by putting print statements in your __init__.py files
Yes. Try this in the interpreter:
import os.path
dir
os
As this shows, os is present in the main namespace.
import myModule as myModule
with this code works import and I can make my doc
import myPackage.myModule as myModule
with this I get
"No module named myPackage.myModule"
Doesn't matter if this file exist in root directory or in myPackage directory.
In RST-File I have not mentioned about myModule, I want to document other file that just import this module.
Sphinx needs to be able to import your code, to generate documentation for classes and functions. You probably need to add your project's root folder to sys.path in Sphinx. You can do this from the Sphinx conf.py file:
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
Replace '..' with the relative path to the project root.