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.
Related
I have a directory like this:
-RootFolder/
- lib/
- file.py
- source/
- book.py
I'm at the book.py, how do I import a class from the file.py?
When you are running book.py, only it's directory is gonna be added to the sys.path automatically. You need to make sure that the path to the RootFolder directory is in sys.path(this is where Python searches for module and packages) then you can use absolute import :
# in book.py
import sys
sys.path.insert(0, r'PATH TO \RootFolder')
from lib.file import ClassA
print(ClassA)
Since you added that directory, Python can find the lib package(namespace package, since it doesn't have __init__.py)
If you start your program from RootFolder (e.g. python -m source.book), the solution by Irfan wani should be perfectly fine. However, if not, then some hacking is required, to let Python know lib also contains relevant code:
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent / "lib"))
import file
from lib.file import some_module
I think this should work fine if the rootfolder is added to path.
If not, we need to do that first as shown by #Amadan and #SorousH Bakhtiary (just wanted to add some explanation);
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
So here, Path(__file__) gives the path to the file where we are writing this code and .parent gives its parent directory path and so on.
Thus, str(Path(__file__).parent.parent) gives us path to the RootFolder and we are just adding that to the path.
I have following directory structure,
/
__init__.py
__meta__.py
I tried to import __meta__ in a file but python complains about not finding a module named __meta__. I checked the current working directory for the file useing os.getcwd() which printed the directory with __meta__.py.
But I can import the module from python interpreter.
Append it to sys path first, and then try to import your module
import os
import sys
sys.path.append(os.getcwd())
Suppose I have the following Django project:
/ # Project dir
/myapp # App dir
/myapp/views.py # views file
/myapp/mymodule.py # a Python module
/myapp/management/commands/mycommand.py # command file
In the views file, I can import mymodule.py by simply writing import mymodule in views.py. However, if I do the same in mycommand.py, I get the error: ImportError: no module named mymodule. I know that to import a model, I can write from myapp.models import mymodel, but mymodule is not a model, it is a separate Python module. So, how do I import this moduel into my command file?
Thats because of where mymodule is in relation to all of your other files.
If you type
from myapp import mymodule
In mycommand.py as long as you have set up your __init__.py files correctly in myapp and are using a setup.py file you should be fine. The modules documentation has more information on this
The fact that you are unable to do this means your myapp folder is not in the python's sys.path. The reason it worked in views.py is because mymodule.py is in the current directory of views.py
Assuming your Project dir (/) is in the python's sys.path:
Add a file __init__.py in myapp directory. This file can be blank.
Then you can do import myapp.mymodule or from myapp import mymodule
The same logic applies for n levels deep. If you have myapp/myappdir1/myappdir2/myfile.py you can do import myapp.myappdir1.myappdir2.myfile assuming each of these have __init__.py file in them.
Check https://docs.python.org/2/tutorial/modules.html#the-module-search-path for python's module search path
I am creating a python module. To test it i put the file in the same directory and then wrote the code
import mymodule
mymodule.dofunction
python then said >>>no module named mymodule but they are in the same directory.
Adapting from previous answer here. Explicitly state that you want to use the current directory.
Also, consider that you need an "__init__.py" file in each directory you are importing from.
import os, sys
lib_path = os.path.abspath('.')
sys.path.append(lib_path)
import mymodule
More information on the import system here.
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