I have a directory structure like this which houses django MVP files - b and c are individual apps on the main website:
a/b/models.py
a/c/models.py
In c/models.py, I can do "from b.models import "ClassName"", but when I do "from c.models import "Class2Name"" in b.models.py, I get the error:
ImportError: cannot import name "Class2Name"
I see this might be a circular dependency issue, but I'm not clear if it is.
Related
I have the following Python package with 2 moludes:
-pack1
|-__init__
|-mod1.py
|-mod2.py
-import_test.py
with the code:
# in mod1.py
a = 1
and
# in mod2.py
from mod1 import a
b = 2
and the __init__ code:
# in __init__.py
__all__ = ['mod1', 'mod2']
Next, I am trying to import the package:
# in import_test.py
from pack1 import *
But I get an error:
ModuleNotFoundError: No module named 'mod1'
If I remove the dependency "from mod1 import a" in mod2.py, the import goes correctly. But that dependency makes the import incorrect with that exception "ModuleNotFoundError".
???
The issue here is that from mod2 perspective the first level in which it will search for a module is in the path from which you are importing it (here I am assuming that pack1 is not in your PYTHONPATH and that you are importing it from the same directory where pack1 is contained).
This means that if pack1 is in the directory /dir/to/pack1 and you do:
from mod1 import a
Python will look for mod1 in the same directory as pack1, i.e., /dir/to/pack1.
To solve your issue it is enough to do either:
from pack1.mod1 import a
or in Python 3.5+
from .mod1 import a
As a side note, unless this is a must for you, I do not recommend designing your package to be used as from pack import *, even if __all__ exists to give you better control of your public API.
my file directory is as follows
mein_paket
--src
----__init__.py
----script.py
--tests
----test.py
In my tests/test.py file, I have the following code:
from mein_paket.src.script import Cat
c = Cat('aicri')
c.meow()
However I get the error ModuleNotFoundError: No module named 'mein_paket'
I have tried different importing methods such as
from mein_paket.src.script import Cat
from mein_paket.src import Cat
from src.script import Cat
from src import Cat
But none seem to work, I'm also trying to avoid the use of sys.append.
I am getting an error ImportError: cannot import name 'dataset_builder'. The import command is following:
from object_detection.builders import dataset_builder
The file tree looks following:
-ObjectDetection
-train.py
-models
-research
-object_detection
-builders
- __init__.py
-dataset_builder.py
I am running the train.py from the root directory (os.getcwd() returns following path C:\Users\horakm\PyCharmProjects\ObjectDetection) and I added in the train.py following code to add paths:
sys.path.append(r'C:\Users\horakm\PyCharmProjects\ObjectDetection\models')
sys.path.append(r'C:\Users\horakm\PyCharmProjects\ObjectDetection\models\research')
sys.path.append(r'C:\Users\horakm\PyCharmProjects\ObjectDetection\models\research\slim')
When I print all paths using sys.path i get this:
['C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\python36.zip',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\DLLs',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages\\win32',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages\\win32\\lib',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages\\Pythonwin',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-packages',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-
packages\\object_detection-0.1-py3.6.egg',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-packages\\win32',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-
packages\\win32\\lib',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-packages\\Pythonwin',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection\\models',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection\\models\\research',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection\\models\\research\\slim']
How is it possible that the import statement doesn't work?
Which object are you actually trying to import?
All that are defined in dataset_builder? In that case it should be from object_detection.builders.dataset_builder import *.
Or is the object also called dataset_builder? In that case it is from object_detection.builders.dataset_builder import dataset_builder.
I'm new to python and I'm having this problem that I can't figure it out.
My file structure is:
enter image description here
On Criador.py I have several functions, for example:
def doSomething():
pass
def doSomethingElse():
pass
and Im trying to use one of this functions on the Controller.py file:
The first thing I did was, on the Controller.py:
import Controller.Criador
and then tried to use that function as:
Controller.Criador.doSomething()
After running Controller.py, I got this error:
ModuleNotFoundError: No module named 'Controller.Criador'; 'Controller' is not a package
I tried several other things, like:
from . import Criador
or
from Controller.Criador import doSomething
or
from Controller import Criador
and nothing helped, just changed the errors to:
ImportError: cannot import name 'Criador'
and
ModuleNotFoundError: No module named 'Controller.Criador'; 'Controller' is not a package
and
ImportError: cannot import name 'Criador'
Can someone give me a light about this? I'm using PyCharm and it does not give me any error when I declare the imports, only when I run the file
If Controller.py and Criador.py are in the same folder, you can do this inside Controller.py:
import Criador
Criador.doSomething()
OK, I have the following directory structure (it's a django project):
-> project
--> app
and within the app folder, there is a scraper.py file which needs to reference a class defined within models.py
I'm trying to do the following:
import urllib2
import os
import sys
import time
import datetime
import re
import BeautifulSoup
sys.path.append('/home/userspace/Development/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
from project.app.models import ClassName
and this code just isn't working. I get an error of:
Traceback (most recent call last):
File "scraper.py", line 14, in
from project.app.models import ClassName
ImportError: No module named project.app.models
This code above used to work, but broke somewhere along the line and I'm extremely confused as to why I'm having problems. On SnowLeopard using python2.5.
import sys
sys.path.append ('/path/to/the/project')
from django.core.management import setup_environ
import settings
setup_environ(settings)
from app.models import MyModel
Whoa whoa whoa. You should never ever have to put your project name in any of your app code. You should be able to reuse app code across multiple projects with no changes. Pinax does this really well and I highly recommend checking it out for a lot of django best practices.
The worst thing you could do here is to hard code your absolute path into your app or settings. You shouldn't do this because it will break during deployment unless you do some import local_settings hacking.
If you have to access the project root directory, try what pinax has in settings.py...
import os.path
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
The thing is that it looks like you are trying to access the models module within the same app and this is waaay easier.
To import models.py inside scraper.py in the same directory just use import models or import models as app_models if you already have something named models in scraper.py (django.db.models for instance). Are you familiar with Python module conventions?
However, the best way is probably to stick with the django idiom, from ... import ... statement:
from app import models
If this doesn't work automatically, then something is wrong in your settings.py.
You don't indicate if project is located in /home/userspace/Development/. I'll assume that it is.
Make sure there's an (empty by default) file named __init__.py in project and another one in app.
EDIT: Next thing to try: Fire up the Python command line in the script's directory and try the following:
import project
import project.app as app
import project.app.models as models
models.__dict__.keys()
Do they all work? If so, what is the last line's output? If not, which dies first?