I define a library with this architecture:
group
├── __init__.py
└──package1
├── __init__.py
└── package2
├── __init__.py
├── file1.py
├── file2.py
└── file3.py
I package this with the command:
python3 setup.py sdist bdist_wheel
And finally push it into a repository (artifactory).
In an other project, I get this dependency, defined in a requirements.txt, and I can find it in the venv folder.
In this project, I have the almost same architecture:
group
├── __init__.py
├── main.py
└──package1
├── __init__.py
└── package2
├── __init__.py
├── file6.py
├── file5.py
└── ...
The problem is: when I run my init test in second project, python complains about ModuleNotFoundError: No module named 'group.package1.package2'.
What did I miss?
Please, if you need more information, don't hesitate to ask ;)
Thanks
Related
I have the following directory structure:
project
├── LICENSE.srt
├── README.md
├── pyproject.toml
├── setup.cfg
└── src
├── bin
│ └── a.out
└── package
├── __init__.py
└── module.py
I am trying to package the above python project with setuptools.
I want to execute the a.out file from module.py for which I am using the subprocess module.
How can I get the path for the a.out file?
I am trying to better understand importing modules. I read about how to do this from here https://stackoverflow.com/a/14132912/14179793 and I can get it to work using solution 1. There is an additional variable that I need to figure out though.
This is a dummy project I am testing with:
.
├── a_package
│ ├── __init__.py
│ └── lib_a.py
├── b_package
│ ├── __init__.py
│ └── test_script.py
├── main.py
└── src
└── src_lib
└── src_lib.py
With this setup I can do:
python -m b_package.test_script
this is lib a function
This is src_lib_function.
test_script.py:
from a_package.lib_a import lib_a_function
from src.src_lib.src_lib import src_lib_function
if __name__ == '__main__':
lib_a_function()
src_lib_function()
pass
The goal is to make b_package/test_script.py executable without using python test_script ie ./test_script
However, adding the shebang at the top #!/usr/bin/env python causes an import error:
$ ./b_package/test_script.py
Traceback (most recent call last):
File "./b_package/test_script.py", line 2, in <module>
from a_package.lib_a import lib_a_function
ModuleNotFoundError: No module named 'a_package'
I assume it is because python is not loading it as a module based off the above mentioned question but I am not sure how to resolve this.
I ended up using setuptools as suggested by kosciej16 to achieve the desired results.
New project structure:
.
├── a_package
│ ├── __init__.py
│ └── lib_a.py
├── b_package
│ ├── __init__.py
│ └── test_script.py
├── main.py
├── pyproject.toml
├── setup.cfg
└── src
├── __init__.py
└── src_lib
├── __init__.py
└── src_lib.py
setup.cfg:
[metadata]
name = cli_test
version = 0.0.1
[options]
packages = find:
[options.entry_points]
console_scripts =
test_script = b_package.test_script:main
This allows the user to clone the repo and run pip install . from the top level then they can execute the script by just typing test_script
I installed a package in my anaconda environment by entering the following line:
pip3 install -e git+https://github.com/gauravmm/jupyter-testing.git#egg=jupyter-testing
I keep getting ModuleNotFoundError: No module named 'testing' on line: from testing.testing import test. I have no idea why this is happening, and believe it has something to do with the way my directory structure is set up.
My directory tree looks like this:
├── hw1_get_started.ipynb
├── requirements.txt
└── src
└── jupyter-testing
├── jupyter_testing.egg-info
│ ├── dependency_links.txt
│ ├── PKG-INFO
│ ├── SOURCES.txt
│ └── top_level.txt
├── LICENSE
├── README.md
├── setup.py
└── testing
├── __init__.py
└── testing.py
I am trying to use this module : https://github.com/gauravmm/jupyter-testing.git#egg=jupyter-testing to do some testing in an online class.
I appreciate any help and explanation as to what I am doing wrong! :)
I have created a kivy app in package using the structure:
.
├── bin
│ └── myapp-0.1-debug.apk
├── buildozer.spec
└── kivy_app
├── __init__.py
├── __main__.py
├── main.py
└── source
└── kivy_app.py
I wish to use some features contained in another package that I have written:
.
└── python_pkg
├── __init__.py
└── source
└── version.py
The module version.py contains:
VERSION = '0.0.1'
When I insert the line:
from python_pkg import VERSION
into kivy_app.py, it works perfectly in kivy on the PC.
I have added python_pkg to the requirements line in buildozer.spec.
I can build an APK, but the application will not run on Android. The logcat output at the point of failure is:
ImportError: cannot import name 'VERSION' from 'python_pkg' (/data/user/0/org.test.myapp/files/app/_python_bundle/site-packages/python_pkg/__init__.pyc)
The _init_.py module in python_pkg has the line:
from python_pkg.source.version import VERSION
How must arrange the setup up so that it works?
Thanks to #inclement I have a solution. I have created a symlink to python_pkg and placed it in the kivy_app directory. The structure of the application is now
.
├── bin
│ └── myapp-0.1-debug.apk
├── buildozer.spec
└── kivy_app
├── __init__.py
├── __main__.py
├── main.py
├── python_pkg -> /home/jeff/projects/kivy_pkg/python_pkg/python_pkg
└── source
└── kivy_app.py
The element python_pkg is not needed in the requirements line in buildozer.spec and I have removed it.
I have a project with multiple subdirectories like so:
/opt/exampleProject/src
├── __init__.py
├── dir1
│ ├── __init__.py
│ ├── file.py
│ └── file2.py
└── dir2
├── __init__.py
├── file3.py
└── file4.py
My main.py file lives here
/usr/bin/main.py
I wanted to know the cleanest way to import exampleProject to be used by main. The fileX.py files also import each other and there are a lot more then shown here. What I would like to be able to do is add this to my $PYTHONPATH so that the main.py can just import them. Is there anyway to do this ?
I have been thinking of adding them all individually with.
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
But I was hoping there was a nice way to do this.
Messing with sys.path is strongly discouraged. Instead create a package for /opt/exampleProject by creating a setup.py.
/opt/exampleProject
├── setup.py
└── src
├── __init__.py
├── dir1
│ ├── __init__.py
│ ├── file.py
│ └── file2.py
└── dir2
├── __init__.py
├── file3.py
└── file4.py
afterwards install it using
pip install -e /opt/exampleProject
After doing that you can simply do
import example_project
in any other python script or package.