ImportError: No module named for my code on Readthedocs - django

I am trying to link my Sphinx documentation with ReadtheDocs. I can build the documentation locally but when I try to have ReadtheDocs automatically generate the documentation I get the following error:
Sphinx Standard Error
Making output directory...
Exception occurred:
File "/var/build/user_builds/mousedb/checkouts/latest/Docs/source/conf.py", line 25, in <module>
from mousedb import settings
ImportError: No module named mousedb
The full traceback has been saved in /tmp/sphinx-err-n_8fkR.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
Either send bugs to the mailing list at <http://groups.google.com/group/sphinx-dev/>,
or report them in the tracker at <http://bitbucket.org/birkenfeld/sphinx/issues/>. Thanks!
My project name is mousedb. I don't understand why I get this import error in the auto-build but not locally.
Update
Based on the comments I think that this is an issue for importing my settings file into a sibling Docs directory. Rather than doing an absolute import (as I had been doing) I should be doing a relative import based on the location of settings.py and conf.py.
I want to import my settings file into my conf.py with the following directory structure:
-mousedb
--settings.py
-Docs
--source
---conf.py
--build

You originally talked about a "local absolute path to my code" and now about setting up relative paths to your code. This probably means you're not using a setup.py file and also not a virtualenv.
In the same directory as Docs/ and mousedb/, add a setup.py:
from setuptools import setup
setup(name='mousedb',
version='0.1',
description="My sample package",
long_description="",
author='TODO',
author_email='todo#example.org',
license='TODO',
packages=['mousedb'],
zip_safe=False,
install_requires=[
'Django',
# 'Sphinx',
# ^^^ Not sure if this is needed on readthedocs.org
# 'something else?',
],
)
After committing/pushing/whatever this file, you can go to your readthedocs settings for your project. Enable the "use virtualenv" setting. This will "nstall your project inside a virtualenv using setup.py install".
The end result is that everything python-related that readthedocs does will have your project in it's sys.path.
The probable cause of your problems is that you run sphinx from within your "root" directory on your local system, where python magically finds the mousedb/ package in your current directory. But readthedocs apparently runs it from within the Docs/ directory and thus cannot find mousedb.

Related

Adding a JSON data file to GCP Dataflow package and using it inside the code

I want to add a JSON file to my Dataflow (Apache Beam) package and use it inside the code.
I've seen several questions on Stack Overflow with different answers, and I tried the recommended approach with a MANIFEST.in and adding data_files to the setup.py file. But nothing I tried works for me.
Here is my current setup.
(I have mapping.json in both the common folder and the root folder for testing purposes.)
MANIFEST.in
recursive-include common *.json
setup.py
import setuptools
setuptools.setup(
packages=setuptools.find_packages(),
data_files=[
("common", ["mapping.json"])
],
include_package_data=True,
install_requires=[
'apache-beam[gcp]==2.31.0',
'python-dateutil==2.8.1'
],
)
Using the file inside common/config.py
import json
from pathlib import Path
def _load_category_theme_mapping(file_name):
path = Path(__file__).parent / file_name
with path.open('r', encoding='utf-8') as file:
return json.load(file)
mapping = _load_category_theme_mapping("mapping.json")
I'm using Flex Templates to run my Dataflow job and I copy the common folder to the target common folder.
When I run the Dataflow job with this setup, it just throws an error.
FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/lib/python3.7/site-packages/common/category_theme_mapping.json'
I tried moving the .json file outside of the common folder (into the root folder) and changed the code (and the Dockerfile) accordingly to read from the base folder.
Then I changed the setup.py file to have the data_files to (".", ["mapping.json"] and MANIFEST.in to have include *.json, but it still fails.
I also tried without having a MANIFEST.in, but then the launcher fails without any informative log.
Any idea what I'm doing wrong?

Import package within sub-package [duplicate]

Assume I have the following files,
pkg/
pkg/__init__.py
pkg/main.py # import string
pkg/string.py # print("Package's string module imported")
Now, if I run main.py, it says "Package's string module imported".
This makes sense and it works as per this statement in this link:
"it will first look in the package's directory"
Assume I modified the file structure slightly (added a core directory):
pkg/
pkg/__init__.py
plg/core/__init__.py
pkg/core/main.py # import string
pkg/string.py # print("Package's string module imported")
Now, if I run python core/main.py, it loads the built-in string module.
In the second case too, if it has to comply with the statement "it will first look in the package's directory" shouldn't it load the local string.py because pkg is the "package directory"?
My sense of the term "package directory" is specifically the root folder of a collection of folders with __init__.py. So in this case, pkg is the "package directory". It is applicable to main.py and also files in sub- directories like core/main.py because it is part of this "package".
Is this technically correct?
PS: What follows after # in the code snippet is the actual content of the file (with no leading spaces).
Packages are directories with a __init__.py file, yes, and are loaded as a module when found on the module search path. So pkg is only a package that you can import and treat as a package if the parent directory is on the module search path.
But by running the pkg/core/main.py file as a script, Python added the pkg/core directory to the module search path, not the parent directory of pkg. You do have a __init__.py file on your module search path now, but that's not what defines a package. You merely have a __main__ module, there is no package relationship to anything else, and you can't rely on implicit relative imports.
You have three options:
Do not run files inside packages as scripts. Put a script file outside of your package, and have that import your package as needed. You could put it next to the pkg directory, or make sure the pkg directory is first installed into a directory already on the module search path, or by having your script calculate the right path to add to sys.path.
Use the -m command line switch to run a module as if it is a script. If you use python -m pkg.core Python will look for a __main__.py file and run that as a script. The -m switch will add the current working directory to your module search path, so you can use that command when you are in the right working directory and everything will work. Or have your package installed in a directory already on the module search path.
Have your script add the right directory to the module search path (based on os.path.absolute(__file__) to get a path to the current file). Take into account that your script is always named __main__, and importing pkg.core.main would add a second, independent module object; you'd have two separate namespaces.
I also strongly advice against using implicit relative imports. You can easily mask top-level modules and packages by adding a nested package or module with the same name. pkg/time.py would be found before the standard-library time module if you tried to use import time inside the pkg package. Instead, use the Python 3 model of explicit relative module references; add from __future__ import absolute_import to all your files, and then use from . import <name> to be explicit as to where your module is being imported from.

tryton file not found when opening database - looking in site-packages rather than directory in PYTHONPATH

tryton V3.8 on MAC. I have a database "steve" with trytond running TCP/IP. When doing database connect in tryton client, I get the following result as the last line in the error window: "IOError: File not found : /site-packages/trytond/modules/product/tryton.cfg"
In ~/.bash_profile, I have:
export PYTHONPATH=~/bryants:$PYTHONPATH
where ~/bryants has a init.py file, and all modules are beneath it.
There is a tryton.cfg file in the ~/bryants/product directory. Why isn't it looking in the ~/bryants directory? Why isn't it being found?
Tryton search modules only in the subdirectory modules from its installation path or for the entry points trytond.modules But it does not use the PYTHONPATH to detect modules.
So you must move the modules under the "modules" directory or you must install the modules with python setup.py install (or python setup.py develop).

How to run django.setup() properly from within a stand-alone python file inside of a django app (on linux-ubuntu)?

Im trying to run a stand-alone python file in a django project. The problematic code is below (standalone.py):
import os
import django
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") tried as well
os.environ["DJANGO_SETTINGS_MODULE"] = "myproject.settings"
django.setup()
The error i receive is: 'No module named myproject.settings'. Somehow the project settings file is not being recognized.
The file runs just fine on my local machine however the problem occurs when running the file on a linux-ubuntu server.
This file is being run from within an app: myproject>myapp>standalone.py
When i move this file to the same directory that myproject resides in, the file runs just fine, so im assuming that the myproject.settings module is not being recognized from within the app directory.
As a temp fix:
sys.path.append('path_to_myproject/')
seems to resolve the issue, but definitely not something i want in production code. Any suggestions on how to resolve this issue? Thanks in advance
If the settings module is myproject.settings, then the outer myproject directory must be on the python path. If the script is in the outer myproject directory (the one that contains manage.py) then you don't need to do anything because the current directory is on the path. Otherwise, you have to manually add the directory to the python path.
The way to add something to the python path is sys.path.append(), so you can't avoid having that in production code (unless you use the PYTHONPATH environment variable instead). Perhaps you would be happier with adding the parent directory instead of hardcoding the path.
sys.path.append('..')

Can't import sqldf in one python file while it works in another. Both files are in different folders. How can I set this right?

In one python file when I try from pandasql import sqldf it works. The path for that is
C:\Users\AmitSingh\Anaconda2\python.exe "C:/Users/AmitSingh/PycharmProjects/HelloPython.py/exercise 2"
In another file when I use the same command it gives me the error
ImportError: cannot import name sqldf
The path for this file is
C:\Users\AmitSingh\Anaconda2\python.exe C:/Users/AmitSingh/Desktop/Data/Udacity/Intro_Machine_Learning/ud120-projects/datasets_questions/explore_enron_data.py
I don't understand why? When I write import sqldf the prompt shows sqldf as an autocomplete option. But doesn't work.
By default, you can't. When importing a file, Python only searches the current directory, the directory that the entry-point script is running from, and sys.path which includes locations such as the package installation directory (it's actually a little more complex than this, but this covers most cases).
However, you can add to the Python path at runtime:
# some_file.py
import sys
sys.path.insert(0, '/path/to/application/app/folder')
import file