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.
Related
I have a problem where this is my project structure:
.
├── Resources/
├── src/
│ ├── __init__.py
│ ├── main.py
│ ├── utils/
│ │ ├── __init__.py
│ │ ├── util.py
│ │ └── otherUtil.py
│ └── calculations/
│ ├── __init__.py
│ └── financials.py
└── tests/
├── __init__.py
└── test.py
My problem is that I can't reach the classes from the src/ folder from the tests, although the code in src/ can reach the Resources folder, through the first shown method.
I have tried:
To append the home library path this way:
Here I used the from src import util after these lines, I even tried from .src import util.
Then this way:
Here I used the from src import util after these lines, I even tried from .src import util.
Than without the sys.path.append() with no use.
I have tried every combination I know, but for no use, and I don't want to install them as individual packages. Does someone have an idea, witch will solve my problem?
Clarification edit:
I don't want to put the tests in the source folder, i want to keep them separate.
You can use this code found here:
# test.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/utils/')
import utils
I'm trying to import a module from outside its directory in a.py.
The directory looks something like this:
.project
├── folder_1
│ └── a.py
|
├── folder_2
│ ├── __init__.py
│ └── b.py
My code in a.py
#contents of a.py
from ..folder_2 import b.py
But from this, I'm getting this error
ImportError: attempted relative import with no known parent package
I've been searching for a solution for this problem for quite a while but I haven't been able to find anything that helps.
With the following structure:
├── project
├── __init__.py
├── folder_1
│ ├── __init__.py
│ └── a.py
├── folder_2
│ ├── __init__.py
│ └── b.py
└── main.py
your a.py will not complain when executing main.py:
from project.folder_1 import a
Since project is a package for main.py and folder_1 and folder_2 are subpackages of it, you can use
Intra-package References.
If you want to directly execute a.py, you can simply do the following:
import sys
sys.path.append('..')
from folder_2 import b
I am currently writing a library in Python. I am trying to use a class that I have defined in a module but I am unable to use it in my main.py. Inside Selector and SeasonSelector I have 2 classes defined with the same name of the file. I get the following error:
No name 'Selector' in module 'Selectors'
main.py:
import pyf1
testSeasonSelector = pyf1.Selectors.SeasonSelector()
testSelector.loadData()
pyf1/__init__.py:
from Selectors.Selector import Selector
from Selectors.SeasonSelector import SeasonSelector
Directory
├── main.py
└── pyf1
├── Selector.pyc
├── Selectors
│ ├── SeasonSelector.py
│ ├── SeasonSelector.pyc
│ ├── Selector.py
│ ├── Selector.pyc
│ └── __init__.pyc
├── __init__.py
├── __init__.pyc
├── __pycache__
│ └── __init__.cpython-38.pyc
└── data
Your directory should look like this
├── main.py
└── pyf1
├── selector.pyc
├── selectors
| ├── __init__.py
│ ├── season_selector.py
│ ├── season_Selector.pyc
│ ├── selector.py
│ ├── selector.pyc
│ └── __init__.pyc
├── __init__.py
├── __init__.pyc
├── __pycache__
│ └── __init__.cpython-38.pyc
└── data
Your directory is not a certified package without init.py as such you cannot make import statements. I also took the liberty of correcting how you named your modules to fit good python naming convention\practices. With this you should be able to import from your built package without issues
I managed to find a solution.
The issue was I wasn't using the correct syntax. Once I added the missing __init__() files I used the import statement Import .parent_file from ParentClass Which then allowed me to pass functionality up the chain how I wanted.
Inside the Selectors directory I could then use from .selector import __Selector to use in SeasonSelector
Furthermore - I hadn't included the PyF1 in my sys.path, therefore python wasn't able to scan the directories. This included with the syntax above allowed me to do what I wanted.
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 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