Hey friends i set my python path as
/home/rohit/test
Here is the structure my test folder
test/
__init__.py
meetinghandler/
__init__.py
meetinghandler.py
db/
__init__.py
models.py
setting.py
manage.py
My problem is when i am trying to import
from test.meetinghandler import meetinghandler
from models.py
i am getting error i.e;
ImportError: No module named test.meetinghandler
please help me out what i am doing wrong .
from test import meetinghandler looks for a test module, which it will not find in your python path.
The reason? You added /home/rohit/test/ to your python path, but that directory doesn't itself contain a test directory :-) Python looks for modules inside the path, that's why.
In your case you'd have to add your home dir to your python path to be able to find it. (Or better, google for virtualenv and so).
Try debugging from the console.
See if the import test works without error. If so, likely there is some error statement in your meetinghandler.py that is not loding the file.
If the import test itself fails on the console, see if the test is in the python path (i.e, as a part of the folder that is present in the settings.INSTALLED_APPS)
Related
I have a module I have installed called lts_fits, and this is its path:
~/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/lts_fits
So it is clearly in the site packages folder. Within this folder, there is a python script:
lts_linefit.py
Yet when I have this line of code in my script:
from lts_fits import lts_linefit
I get this error:
ImportError: No module named lts_fits
How? It's clearly in there, and I have tried this same syntax with other random scripts and they import just fine. For instance, a file abc.py located in the folder ~/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/sympy imports just fine when I have the line from sympy import abc. What could be going wrong?
You need an __init__.py file in that directory (you do not have to put anything into the file, all you need to do is create it).
The easiest way to create said file is by using:
touch __init__.py
from within your lts_fits directory in your command line/terminal/console.
See this SO article: What is __init__.py for?
And the Python Documentation for packages.
I have been looking for a way to solve this.
I have a python project, and this is the folder structure I want:
/project/main.py
/project/src/models.py
/project/test/tests.py
I want to be able to run the tests by executing the tests.py in terminal. tests.py imports modules in /project/src/ for testing. First I solved this by adding
sys.path.insert(0, '..') in tests.py. But then the paths used in models.py for opening text files had to be relative to the tests.py, etc. Which means the program wouldn't run when excecuted from main.py, cause of the paths.
I also tried with dots when importing modules into tests.py, like from ..src.models import *, but that gave error message saying "Attempted relative import in non-package".
What should I put in the top of tests.py to be able to import the modules from models.py?
The structure you're using is not one I would recommend, but I'm a comparaitive newb to how Python projects are usually structured. I believe this will do what you're after:
1) Place an __init__.py file inside /project, /project/src, and /project/test to make sure they're treated as packages.
2) Place from __future__ import absolute_import at the top of each Python file.
3) Then use relative imports:
test.py:
from ..src import models
main.py:
from .src import models
4) You'll need to start your application differently. Ensure your current directory is the parent of /project (which appears to be the file system root) and run your project this way:
python -m project.main
For my own project, I would definitely put main.py inside src if it's the start point of your application. I might put tests.py in src, too, but if not, I would add /project/src to the test runner's Python path on the command line instead of in code.
I would still use absolute_import regardless. In my experience, it's a very clean solution to module organization, and it's also how things work by default in Python 3.
first put your main.py in the src directory..
in your tests you can do , sys.path.append('the src directory')
if you like to force the execution to be in specific directory regardless from where you are executing the app i suggest you adding
import os
os.chdir('relative path to the src dir')
thisway your program will run in the directory you specified so it will respect the relative paths you have in your code
Suppose I have the following directory structure:
workspace/
__init__.py
ys_manage/
__init__.py
manage.py
ys_utils/
__init__.py
project_dicts.py
Now, suppose I need access to project_dicts.py in manage.py. Also, my $PATH includes /home/rico/workspace/ys_manage.
I need to be able to run manage.py from any directory on my machine and still be able to access project_dicts.py.
My $PYTHONPATH only has /home/rico/workspace.
If I include the following in manage.py I can run the file from ~/workspace/ys_manage but not anywhere else.
import sys
sys.path.append('..')
from ys_utils import project_dicts
It appears that the '..' gives a relative path to where the directory where the file is run, not where the file is located. Is this correct?
I wanted to try and use ys_manage/__init__.py to import project_dicts.py so that it would be available in manage.py universally. Is this a good idea?
I've never used __init__.py for anything other than a "package creator". That is, I've never used it for initialization purposes. Perhaps I'm doing it wrong.
Contents of ys_manage/__init__.py:
import sys
sys.path.append('..')
from ys_utils import project_dicts
Should I include something in manage.py to look for this import?
When I try and run manage.py I get the following error:
NameError: global name 'project_dicts' is not defined
As a secondary question, do I need to have workspace/__init__.py? I'd really rather not have it because ys_manage and ys_utils (and about a dozen other packages) are all under revision control and used by several other developers...workspace is not.
Generally, I've found trying to use relative paths for imports dangerous and very error prone. I'd suggest just putting workspace on your PYTHONPATH (or adding it programatically in __init__.py) and importing everything relative to that static location. It will make your code more easily readable too, as you'll be able to track down where imports are coming from much more quickly and clearly.
Try this instead of sys.path.append('..'):
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
(you'll need to import both sys and os).
I have checked as many SO pages as I could and tried everything I found, but none have been successful. I also checked the PEP page regarding importing and tried every example, none of which worked.
I have a tests folder with unit tests in them, and I need to import the module I want to test. The modules are in a folder called 'src' which is next to the src folder.
The folders/files look something like this:
Project /
src /
stringbuilder.py
__init__.py
tests /
stringbuilder_test.py
__init__.py
main.py
__init__.py
I have tried everything I could: adding __init__.py to every folder making it a module including the project's main folder.
import src.module_to_test
from ..src.module_to_test import function_to_test
from ..src import module_to_test
I have tested all other combinations all of which have failed. I am starting to think there must be something wrong with either my settings or understanding-- I thought this was suppose to be simple.
If I am making any obvious errors please let me know.
from tests import stringbuilder
Error Message:
$ ./stringbuilder_test.py
Traceback (most recent call last):
File "./stringbuilder_test.py", line 14, in <module>
from tests import stringbuilder
ImportError: No module named tests
The same error occurs for (but tests = src):
from src import stringbuilder
If scripts are executed from inside a package, then various hacks need to be employed to get the imports to work properly. Probably the most common of these is to manipulate sys.path:
import sys, os
sys.path.insert(0,
os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src import stringbuilder
del sys.path[0]
There is no way to achieve this using normal import statements.
A generally better solution is to simply avoid running scripts inside packages altogether. Instead, put all scripts outside the package in a containing directory. Given that the directory of the currently running script is automatically added to the start of sys.path, this will guarantee that the package can be always be directly imported, no matter where the script is executed from.
So the directory structure might look something like this:
project /
package /
__init__.py
src /
__init__.py
stringbuilder.py
tests /
__init__.py
stringbuilder_test.py
main.py
test.py
The test.py script can then import its tests like this:
from package.tests import stringbuilder_test
And stringbuilder_test.py can import the src modules like this:
from package.src import stringbuilder
from folder import my_module
If there is a __init__.py file in folder.
Add that folder to your PATH.
I have a module(Executive.py) that I am trying to import into another module I am working on (ExecutiveTest.py). The directory structure is like so:
src/
common/
python/
Executive.py
tests/
ExecutiveTest.py
In ExecutiveTest.py, I have the following line:
from common.Executive import Executive
I get an error saying:ImportError: No module named common.Executive
How do I correct this import error?
You have to have an __init__.py file in the root of your package (it can be empty). Also, your module hierarchy has to reflect the directory structure, so python and tests should be part of the import as well.
I found a similar post here. it looks like you can define the path that python refers to when looking for stuff to import. Something like:
sys.path.append( )