In Python 3.5, for a directory structure that looks like this:
.
└── project
└── __init__.py
└── project.py
└── module1
├── __init__.py
└── module1.py
└── module2
├── __init__.py
└── module2.py
Why do I receive "No module named 'project'" error when calling
import project.module1
from module2.py?
As far as I can tell, the docs say this should work.
Related
I am trying to create a debian package for a postgreSQL extension Apache-age release 1.1.1 and created the directory structure using dh_make command.
The directory structure is as follows:
age-1.1.1 (project root)
├── debian
│ ├── changelog
│ ├── compat
│ ├── control
│ ├── docs
│ ├── examples
│ ├── links
│ ├── manpages
│ ├── menu
│ ├── postinst
│ ├── postrm
│ ├── preinst
│ ├── prerm
│ ├── rules
│ ├── source
│ └── watch
├── src
└── Makefile
The dpkg-buildpackage -b when run from project-root folder it looks for debian folder, then reads the rule file, then reads the Makefile located in the project root to build the package.
I want to change the directory structure to the following:
.project root
├── packaging
│ ├── debian
│ │ ├── control
│ │ ├── control.in
│ │ ├── changelog
│ │ ├── copyright
│ │ ├── pgversions
│ │ ├── rules
│ │ └── ...
│ └──
├── src
├── LICENSE
├── README.md
├── Makefile
└── ...
I want to change the directory structure so that the dpkg-buildpackage -b command can be run from the packaging folder and it should build the package.
Inside your Makefile
Modify the install paths accordingly. It should point to your packaging/debian/* where * is the filename.
This way the Makefile can point to the correct file path target inside the new folder structure.
I'm not sure if this is the best way to do this but it's working for me:
Here are the steps:
First run the dh_make_pgxs command from the project root directory.
Create a packaging directory in the project root and move the debian directory created in step 1 to this directory along with the Makefile, age.control and the age--1.1.1.sql.
Your file structure should look like this:
.project root
├── packaging
│ ├── debian
│ │ ├── control
│ │ ├── control.in
│ │ ├── changelog
│ │ ├── copyright
│ │ ├── pgversions
│ │ ├── rules
│ │ └── ...
│ ├── age--1.1.1.sql
│ ├── age.control
│ ├── Makefile
│ └── ...
├── src
├── LICENSE
├── README.md
└── ...
Change the file paths in the Makefile like:
src/backend/age.o should be ../src/backend/age.o.
./tools/ should be ./../tools/.
and so on.
Now you can simply run the dpkg-buildpackage -b command from the packaging directory to build the debian package.
Note: In step 1 we are running dh_make_pgxs in the project root first, this is to make sure that the project name in the control files and the version in the changelog file are correct. In this case the name/source in control, control.in & changelog files should be apache-age and the version number in changelog file should be 1.1.1-1.
Alternatively, you can run the command from the packaging directory and manually change the name and version in the control and changelog files.
I have been having a lot of trouble trying to run pytest. Basically the importing was not working properly. Let me explain. The folder structure of my project is the following:
src
└── api
├── __init__.py
├── app.py
├── document.py
└── tests
├── __init__.py
└── test_app.py
And app.py has the following import statement:
from document import Document
and obviously test_app.py imports app
Whenever I runned pytest from the src folder it would throw the following error:
ModuleNotFoundError: No module named 'document'
But once I removed the __init__.py file from the api directory it just works out of the box.
src
└── api
├── __init__.py <--- REMOVED
├── app.py
├── document.py
└── tests
├── __init__.py
└── test_app.py
I have been reading many different threads, but none of them explains why this is so.
Anyone understands why?
Sources I have been reading through:
pytest cannot import module while python can
PATH issue with pytest 'ImportError: No module named YadaYadaYada'
https://docs.pytest.org/en/6.2.x/pythonpath.html
I'm using a Twitter engineered build tool pants to manage many projects inside my monorepo. It outputs .pex files when I complete a build, this is a binary that packages the bare minimum dependencies I need for each project and makes them a "binary" (actually an archive that's decompressed at runtime), my issue is a utility that my code has used for a long time fails to detect some .json files(now that I'm using pants) I have stored under my environments library. all my other code seems to run fine. I'm pretty sure it has to do with my config, perhaps I'm not storing the resources properly so my code can find it, though when I use unzip my_app.pex the resources I desire are in the package and located in the proper location(dir). Here is the method my utility uses to load the json resources:
if test_env:
file_name = "test_env.json"
elif os.environ["ENVIRONMENT_TYPE"] == "PROD":
file_name = "prod_env.json"
else:
file_name = "dev_env.json"
try:
json_file = importlib.resources.read_text("my_apps.environments", file_name)
except FileNotFoundError:
logger.error(f"my_apps.environments->{file_name} was not found")
exit()
config = json.loads(json_file)
here is the the BUILD file I use for these resource currently:
python_library(
dependencies=[
":dev_env",
":prod_env",
":test_env"
]
)
resources(
name="dev_env",
sources=["dev_env.json"]
)
resources(
name="prod_env",
sources=["prod_env.json"]
)
resources(
name="test_env",
sources=["test_env.json"]
)
and here is the BUILD file for the utility that calls these resources of which the python code above is what you saw:
python_library(
name="environment_handler",
sources=["environment_handler.py"],
dependencies=[
"my_apps/environments:dev_env",
"my_apps/environments:prod_env",
"my_apps/environments:test_env"
]
)
I always get an FileNotFoundError exception and I'm confused because the files are available to the runtime, what's causing these files to not be accessible? and is there a different format I need to set up the JSON resources as?
Also for context here is the decompressed .pex file(actually just the source-code dir):
├── apps
│ ├── __init__.py
│ └── services
│ ├── charts
│ │ ├── crud
│ │ │ ├── __init__.py
│ │ │ └── patch.py
│ │ ├── __init__.py
│ │ └── main.py
│ └── __init__.py
├── environments
│ ├── dev_env.json
│ ├── prod_env.json
│ └── test_env.json
├── __init__.py
├── models
│ ├── charts
│ │ ├── base.py
│ │ └── __init__.py
│ └── __init__.py
└── utils
├── api_queries
│ ├── common
│ │ ├── connections.py
│ │ └── __init__.py
│ └── __init__.py
├── calculations
│ ├── common
│ │ ├── __init__.py
│ │ └── merged_user_management.py
│ └── __init__.py
├── environment_handler.py
├── __init__.py
├── json_response_toolset.py
└── security_toolset.py
I figured it out: I changed the way I access the files within the library and it works perfectly before and after the build to .pex format. I used:
import pkgutil
#json_file = importlib.resources.read_text("my_apps.environments", file_name)
json_file = pkgutil.get_data("my_apps.environments", file_name).decode("utf-8")
For this Netbeans project:
.
├── build.xml
├── manifest.mf
├── nbproject
│ ├── build-impl.xml
│ ├── genfiles.properties
│ ├── groovy-build.xml
│ ├── private
│ │ └── private.properties
│ ├── project.properties
│ └── project.xml
└── src
├── Config1.groovy
└── net
└── bounceme
└── mordor
└── groovy
└── file_ops
└── NewGroovyScript.groovy
8 directories, 10 files
what would the standard location, or path, for foo.properties so that it can be easily picked up? in src?
You should be able to stick it in the same folder as your NewGroovyScript.groovy class file, then just do:
Properties p = NewGroovyScript.class.getResource('foo.properties').withInputStream { s ->
Properties properties = new Properties()
properties.load(s)
properties
}
I have a collection of .dox documentation files for my project
in a dox directory as illustrated below
In the input section I have included ../ for doxygen to pick up the source code. However when I put ./ it does not pick up my documentation files and have to include each file. Is there a way to include them automatically?
Here is the docs and lib directories. In lib I have the source code, whereas in docs I have the documentation.
../
├── docs
│ ├── dox
│ └── Doxyfile
└── lib
Here is the contents of the dox directory
./dox/
├── gnu_affero_gpl.dox
├── gnu_fdl.dox
├── gnu_gpl.dox
├── larsa
│ └── larsa_core.dox
├── larsa.dox
├── meidum
│ ├── lattices
│ ├── lattices.dox
│ ├── lattices.dox~
│ ├── polyhedra
│ └── polyhedra.dox
├── meidum.dox
├── modules.dox
└── vikingr.dox
I have now fixed the problem. The solution was to remember to add *.dox in FILE_PATTERNS variable in Doxyfile.