This is the structure of my project at the moment.
classes/
├─ __init__.py
├─ card.py
├─ find.py
├─ player.py
├─ table.py
.gitignore
main.py
My __init__.py:
from .card import *
from .player import *
from .table import *
The problem is when I want to import from example player to table.py,
like this:
from classes import Player
from classes import Card, CardType, CardValue, HandValue
I get the following error message:
Traceback (most recent call last):
File "/home/adrian/Desktop/Programozas/Poker/classes/table.py", line 1, in <module>
from classes import Player
ModuleNotFoundError: No module named 'classes'
Do you have any idea?
Notice that this error is occurring in ".../classes/table.py", line 1, as you are trying to import classes.Player when both table.py and player.py are contained in classes. To do this in table.py, you would use from .player import Player just like you did in __init__.py (instead of from classes import Player).
Anything at the same level or above classes can be done using this method, but anything defined inside classes cannot call up to classes, as anything therein does not even know classes exists.
Edit: if you need to be able to run both main.py and table.py, you could do something like:
# in table.py
try:
from player import Player # this should work when running table.py directly
except:
from .player import Player # this should work when running main.py (it's a relative import)
Related
This is my project structure:
maths/
__init__.py
trig.py
coord.py
pitch.py
models/
__init__.py
model.py
In model.py, I have:
import sys
sys.path.append("../")
from maths.pitch import *
if __name__ == "__main__":
# my routines
In pitch.py, I have:
from trig import calculate_distance_angles
When I run model.py, I'm getting the error:
File "model.py", line 38, in <module>
from maths.pitch import *
File "../maths/pitch.py", line 9, in <module>
from trig import calculate_distance_angles
ModuleNotFoundError: No module named 'trig'
How do I fix this?
The error came from here: from trig import calculate_distance_angles. I suppose you are looking for your maths/trig.py file:
maths/
__init__.py
trig.py <-- this one
coord.py
pitch.py
models/
__init__.py
model.py
On your sys.path, you may have this:
(all the original sys.path stuff)...
"../" (added at model.py)
But the maths model, as far as we can see, is not on sys.path. If that's true, you may have to modify the import as follows. This should work:
from maths.trig import calculate_distance_angles
I have a file structure like this:
work/
├─ analysis.ipynb
├─ app/
│ ├─ __init__.py
│ ├─ class_a.py
│ ├─ script.py
│ ├─ utils.py
File class_a.py contains a class MyClass and also an import from utils like this:
from utils import useful_function
class MyClass():
...
Then I try to import MyClass in analysis.ipynb like this:
from app.class_a import MyClass
and get an error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-8eeb8559d767> in <module>
2 import os
3 from datetime import datetime
----> 4 from app.class_a import MyClass
5 from app.utils import useful_function
6 ...
~/Documents/work/app/class_a.py in <module>
1 import pandas as pd
----> 2 from utils import useful_function
3
4 class MyClass():
5 '''indeed very necessary class for me
ModuleNotFoundError: No module named 'utils'
I have figured out that if I change all imports in app folder to something like this:
from app.utils import useful_function
Then I can import all I need from analysis.ipynb
However app should work as something I run with python script.py and that does not work unless imports are written in the original way.
I do not understand modules and packaging and so can not even ask a question precisely, but how do I align import "styles" in order to both be able to run python scripts.py from the app directory and import MyClass from analys.ipynb?
import statements search through the list of paths in sys.path, so I've added these lines to my analysis.ipynb:
import sys
sys.path.append('/path/to/work/app/')
In case someone would have the same question and struggle as me.
I have the following code structure:
Graph API/
│── main.py
├── helper_functions/
├── defines.py
├── insights.py
insights.py imports 2 functions from defines.py at the beginning:
from defines import getCreds, makeApiCall
It then uses "makeApiCall" for this function:
def getUserMedia( params ) :
// Some code about url endpoints etc.
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
I want to use the getUserMedia function in the main.py script, so I import it with:
from helper_functions.insights import *
But I get the error:
Traceback (most recent call last):
File "/Users/Graph_API/main.py", line 1, in <module>
import helper_functions.insights
File "/Users/Graph_API/helper_functions/insights.py", line 1, in <module>
from defines import getCreds, makeApiCall
ModuleNotFoundError: No module named 'defines'
What leads to this error? When I use the getUserMedia function within insights.py it works fine. I already tried importing defines.py to main.py as well, but I get the same error.
I am pretty new to programming, so I would really appreciate your help :)
You should replace
from defines import getCreds, makeApiCall
With
from helper_functions.defines import getCreds, makeApiCall
Or
from .defines import getCreds, makeApiCall
You must give a path either from project directory as in the first example, or from the relative path from the insights file by adding a dot.
You could also consider adding init.py to the helper_functions folder. Checkout here: What is __init__.py for?
I'm trying to build a library with multiple sub-modules. The general structure looks like so:
package_test/
|___ setup.py
|___ README
|___ package_name/
|___|___ __init__.py
|___|___ submodule/
|___|___|___ __init__.py
|___|___|___ superawesome.py
|___|___ submodule2/
|___|___|___ __init__.py
|___|___|___ prettyokay.py
The goal is to allow a user to do something like:
from package_name.submodule import superawesome
from package_name.submodule2 import prettyokay
The __init__.py for each submodule currently contains (or the appropriate name for submodule2):
from .superawesome import superawesome
__all__ = ['superawesome']
The __init__.py for inside package_name contains:
__version__ = 'v0.0.alpha'
__all__ = ['submodule','submodule2']
When I attempt to use this, iPython shows this error:
From package_name.submodule import superawesome
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-03be4241edd5> in <module>()
----> 1 from package_name.submodule import superawesome
ModuleNotFoundError: No module named 'package_name.submodule'
My setup.py currently contains:
from distutils.core import setup
setup(
name = 'package_name',
packages = ['package_name'], # this must be the same as the name above
version = '0.1.1',
long_description=open('README').read(),
)
I need help figuring out how to do the layered importing structure in order to properly import the submodules as part of the whole module. This is clearly a contrived naming set, but in the real problem, each submodule contains a set of modules that belong together... but all of the submodules together are necessary for the library; so I really want to maintain this structure.
In the following structure:
src:
model:
- boardmodel.py
- tests.py
exceptions:
- exceptions.py
- visual.py
I'm running visual.py from the terminal. In my boardmodel.py, I have the following import:
from exceptions.exceptions import ZeroError, OverlapError, ArgumentError, ProximityError
This line causes an error when running visual.py:
Traceback (most recent call last):
File "visual.py", line 3, in <module>
from model.boardModel import Board
File "/Users/sahandzarrinkoub/Documents/Programming/pythonfun/BouncingBalls/balls/src/model/boardModel.py", line 5, in <module>
from exceptions.exceptions import ZeroError, OverlapError, ArgumentError, ProximityError
ModuleNotFoundError: No module named 'exceptions'
What is the correct way to solve this problem? Should I change the import statement inside boardmodel.py to from model.exceptions.exceptions import ZeroError....? That doesn't feel like a sustainable solution, because what if I want to use boardmodel.py in another context? Let me her your thoughts.
EDIT:
I changed the structure to:
src:
model:
- __init__.py
- boardmodel.py
- tests.py
exceptions:
- __init__.py
- exceptions.py
- visual.py
But still I get the same error.
Place an empty file called __init__.py in each of the subdirectories to make them packages.
See What is __init__.py for? for more details
Then, within boardmodel.py you need to either use relative imports
from .exceptions.exceptions import ...
or absolute ones (from the root of PYTHONPATH)
from model.exceptions.exceptions import ...
When you execute a script from the command line, the directory containing the script is automatically added to your PYTHONPATH, and becomes the root for import statements
You are not at the root of your package so you need relative import.
Add a dot before exceptions:
from .exceptions.exceptions import ZeroError, OverlapError, ArgumentError, ProximityError
One dot mean "from the current directory"…