I am using virtual Environment to develop the project. Using python3 and Django 1.9.7
I am splitting views into multiple files. Below is the tree structure.
|-- urls.pyc
`-- Views
|-- DashboardView.py
|-- DashboardView.pyc
|-- __init__.py
|-- __init__.pyc
|-- __pycache__
| |-- DashboardView.cpython-34.pyc
| |-- __init__.cpython-34.pyc
| `-- VehicleView.cpython-34.pyc
|-- VehicleView.py
`-- VehicleView.pyc
Inside __init__.py file -
from VehicleView import *
from DashboardView import *
When I am activating virtual environment and running code it throws me below error -
File "/home/rana/DjangoProject/FirstChoice/MyFirstCar/MyFirstCarBackEnd/Views/__init__.py", line 1, in <module>
from VehicleView import *
ImportError: No module named 'VehicleView'
If I do not activate the virtual environment and run the code, it runs without any error. Default django version 1.8.4 and python 2.7.6
In your __init__.py try to use local imports instead, this may be a problem if you are using python3 in your virtual environment.
from .VehicleView import *
from .DashboardView import *
Besides file and module names in python should follow snake case convention, only classes should use CamelCase.
from .vehicle_view import *
form .dashboard_view import *
Related
there is my project hierachy:
#__init__.py are empty files
folder
|-- globalFunctions.py
|-- __init__.py
|-- monitor
|-- file.py
|-- __init__.py
I'm tring to import functions from globalFunctions.py, when I'm in the file.py file.
I have tried to import it with
from .. import globalFunctions
but I'm getting
ImportError: attempted relative import with no known parent package
Do you know how to make it work?
I am working with ROS packages and coming from this tutorial. This import statement surprisingly works given the absence of AddTwoIntsResponse anywhere in the current working directory or any other directory listed in PATH. Also how come a .srv gets imported?
# add_two_ints_server.py
from beginner_tutorials.srv import AddTwoInts, AddTwoIntsResponse
The current working directory is ~/catkin_ws/src/beginner_tutorials/srv
This is my ROS directory layout:
catkin_ws
|-- src
| `-- beginner_tutorials
| |-- scripts
| | `-- add_two_ints_server.py
| `-- srv
| `-- AddTwoInts.srv
|-- build
`-- devel
The contents of AddTwoInts.srv are:
int64 a
int64 b
---
int64 sum
According to my understanding this should throw an ImportError: cannot import name 'AddTwoIntsResponse', but it doesn't. Importing any other file say: from beginner_tutorials.srv import foo throws an ImportError.
Where is my understanding going wrong?
|-- src
| `-- beginner_tutorials
| |-- scripts
| | `-- add_two_ints_server.py
| `-- srv
| `-- AddTwoInts.srv
|-- build
|-- devel // this is where your modules are imported from
When you build the package using catkin_make, 'catkin` generates the relevant python files for your service type defined in .srv file and puts them under catkin_ws/devel/lib/your-python-version/dist-packages/package-name/srv.
If your workspace is sourced, catkin_ws/devel/lib/your-python-version/dist-packages/ is already added to your PYTHONPATH and that is how you are able to import them successfully.
In case of the tutorial package that you are using, imports may work even when you haven't sourced your current catkin-directory, if you have the binaries of the tutorials installed. This way the python modules reside under /opt/ros/ros-version/lib/your-python-version/dist-packages/ and that is again part of the PYTHONPATH. (If ROS env is available)
I'm using PyCharm CE to develop a project with a structure similar to this:
test_python/
|-- app
| |-- __init__.py
| |-- mymodule.py
| |-- mymodule.pyc
| `-- test_mymodule.py
|-- config.py
`-- tests
|-- __init__.py
|-- test_config.py
`-- test_models.py
When I try to run my test scripts such as test_config.py, I get:
$ python tests/test_config.py
Traceback (most recent call last):
File "tests/test_config.py", line 1, in <module>
from config import app_config
ImportError: No module named config
I have read a lot of other SO posts that talk about needing a init.py file in all directories that are packages (which I have done already). Many also suggest messing around with sys.path. My problem with this latter approach is that I never had to meddle with the paths previously. I'm not sure if it's something that changed with my dev environment setup, but here it is:
Python 2.7 | macOS Sierra | PyCharm CE
I have tried to install a virtual environment with virtualenv but didn't see a difference. Here is the sample project on github if you'd like to run it yourself
It seems that there is a problem with folder depth. Replace from ../config import app_config.
I think it will work if you change the working directory on your run configuration to test_python. All the packages in your import statements are relative to some entry in your Python path, and the working directory is usually in the Python path.
I'm having a tough time understanding packages, and specifically how to use unittest with packages. I looked at this question () but the correct answer to that question didn't solve my problem. I have the following structure:
model
|-- __init__.py
|-- boardmodel.py
|
|-- exceptions
| |
| |-- __init__.py
| |-- exceptions.py
|
|-- test
|-- __init__.py
|-- test_boardmodel.py
With the following files/imports:
model/__init__.py:
import model.exceptions.exceptions
import model.boardmodel
model/exceptions/__init__.py:
contains nothing
model/test/__init__.py:
contains nothing
imports inside boardmodel.py::
from model.exceptions.exceptions import ZeroError, OverlapError, ArgumentError, ProximityError
imports inside test_boardmodel.py:
import unittest
from model.boardmodel import Board, Ball, Wall
from model.exceptions.exceptions import ProximityError
I place myself in the model-directory and I run python -m unittest test.test_boardmodel.py. I get the following message:
ERROR: test_boardmodel (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_boardmodel
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
File "/Users/sahandzarrinkoub/Documents/Programming/pythonfun/BouncingBalls/balls/src/model/test/test_boardmodel.py", line 3, in <module>
from model.boardmodel import Board, Ball, Wall
ModuleNotFoundError: No module named 'model'
I'm a bit lost with how the imports work and what location the modules/packages are looked for when an import statement is executed. Why isn't model found?
I shall add that if I remove model. from all the imports listed, the tests work, but I can't use the package from "outside" anymore:
src
|-- visual.py
|
|-- model
|-- __init__.py
|-- boardmodel.py
|
|-- exceptions
| |
| |-- __init__.py
| |-- exceptions.py
|
|-- test
|-- __init__.py
|-- test_boardmodel.py
inside visual.py:
import model
from model.boardmodel import Board
I was facing the same issue, being able to import some modules from several files, but not from a test file, so I saw this solution:
If you have test/my_test.py, tests should be run as:
python -m test.my_test
After that, I imported what I wanted and got no errors.
Try adding the following above your import statements:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
I believe the standard package structure is
myproject
├── myproject
├── tests
└── scripts
If you want to run the test without installing the package, run them from the top myproject folder so that import myproject will succeed in your tests. (Similarly for the scripts.) For this to work, use absolute imports or explicit relative imports in myproject.
I am trying to implement following hierarchy.
My final goal is myscpt.sh should run from any folder
But I am getting import errors.
What is the best approach to implement such type of hierarchical architecture?
FooSoft
|
|-- foo/
|
|-- test.py
|
|-- common/
|
|-- utils/
|
|-- api/
|
|-- scripts/
|-- myscript.py
|
|-- bin/myscpt.sh
|
|-- etc/foo.conf
bin/myscpt.sh
/usr/bin/python /path/FooSoft/foo/script/myscript.py /path/FooSoft/etc/foo.conf
foo/script/myscript.py
from ..common import *
from ..utils import *
from ..test import *
.
.
<Doing some stuff>
I am using .. import in most of modules to avoid absolute path.
Typically I resolve import errors by always using the package root as a reference.
First, I would include a setup.py file in the project root, and add in a minimal setuptools wrapper.
python setup.py develop
Then you don't have to worry about where you run the script from. Your imports become as follows:
from foo.common import *
from foo.utils import *
from foo.test import *
Explicit relative imports with leading dots like from ..common import anything can be used only from a code that has been imported as a submodule of the package e.g. foo.scripts, but not from the the code imported as __main__ script, even if the script path contains .../foo/scripts/.... The main script must use absolute imports or it can be used as module by something like python -c "from foo.scripts import myscript; myscript.run()". (install the foo or use PYTHONPATH=/path/FooSoft). See Intra-package References in the docs or a similar question.