I've created a new app called engineapp. Inside this app, there is a folder engine which is a Scrapy project.
When I try to import model from storage app inside top.py file, it returns:
from storage.models import TopItem
ImportError: No module named storage.models
Or the similar problem, when I try to import settings of scrapy project:
from engineapp.engine.engine import settings
It returns:
from engineapp.engine.engine import settings
ImportError: No module named engineapp.engine.engine
This is when I run scrapy project from command line.
Both imports created PyCharm itself.
As you can see, I've added __init__() everywhere so python would be able to recognize those files.
Do you know what should I do to be able to import those files?
PyCharm autocomplete relies on different IDE settings. You've marked your realstate_scanner as sources root, so PyCharm can resolve imports for this. From docs:
These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports.
If you can't import some module in python, you should check PATH/PYTHONPATH variables first to make sure python interpreter knows where to find your module.
Related
I have a project with structure like on this picture.
Folders structure
Where 'backend' folder is Django project folder.
I need to import module from another folder 'main' inside Django app file, i.e. import main.Text_Generator in backend.app.views file.
I tried: from ...main.Text_Generator import *. This raise an error while running a server: "attempted relative import beyond top-level package"
And from main.Text_Generator import *, also error "No module named 'main'"
What is the correct way to do such import?
Add this:
import sys
sys.path.append("..")
And then you should be able to get it with:
from main.Text_Generator import *
You're using a module outside your Django project. I would recommend moving the folder inside your project directory [or app directory] rather than messing with your PATH. If you move main inside backend your existing stuff will work.
I want to write my own python package inside django's app. It looks like that: I have secondary app. It totally works except one thing. Someday I'd decided to make my app more structured. And there where problems started. First of all, I've added python package(with simple init.py inside directory) to the app. Then I've added second package inside that package. And when I try to run django.setup() inside packages' files, I'm getting this error: ModuleNotFoundError: No module named '<primary settings container module name goes here>'
How to make my custom package's functions working properly?
The problem is that settings module isn't "viewable" from your package, e.g. you should add the location of main directory to PATH variable, which could be done like this:
from sys import path
from pathlib import Path
path.append(str(Path(__file__).resolve().parent))
Or you can simply add it permanently to system PATH.
I am working on a project based on Python 2.7 and I am trying to import a module which is in a package folder that contains __init__.py and the file that I want to import called package1.py, but I am unable to do so. This is my folder structure: main_project/Tools/common/package1.py
Note that my project files are in the folder main_project. So, I am trying to call the package1.py by using an import statement in my script:
from Tools.common.package1 import variable
But I am getting an ImportError: No module named Tools.common.package1.
What is the solution to solving this error as I want to use the package feature for my project?
Maybe use the solution i found here :
# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
or verify your module has a __init__.py
Importing files from different folder
Ok I have found the answer. I also had to insert an init.py in the folder Tools as well. Initially I only inserted init.py in common but not in Tools as we should also make Tools a package if we want to access common
I've a Python program with the following structure:
sdk
-> jobs
->release
In the release folde I have a script called my_release.py. In that I'm trying to import a property called PROPERTY_1 from sdk/__init__.py. I tried adding from sdk import PROPERTY_1. When I run the script I get ModuleNotFoundError: No module named 'sdk'. How do I import this?
Python looks for modules in sys.path by default. If you want to import modules from a specific path take a look at this:
Importing files from different folder
You should not be in the correct working directory. Just check it with %pwd magic command from ipython
This is probably a noob problem. For that I apologize, but I couldn't find a solution so far.
I short, for some reason that I don't understand, I can't access modules from my src directory in my tests.
My project setup looks like this:
src/package/module.py
tests/package/module_test.py
and my test roughly looks like this:
import package
import unittest
class module_test(TestCase):
def testSomeMethod(self):
m = package.SomeClass() #there is class of that name in module.py
I checked the run configuration setup in PyDev and it says that both src and tests are on the PYTHONPATH when I execute the tests. But when I try to run this test, I get the error 'module' object has no attribute 'SomeClass'.
What am I doing wrong?
When you do import package, you import the package, not the module inside it. If you want to import the module, you need to do from package import module (and then refer to the class as module.SomeClass, not package.SomeClass).
Packages are containers for groups of modules. They don't magically allow you to access everything inside any of the modules (although you can have them automatically import their modules). You still have to import the individual modules in the package.