ImportError under two identical folder - python

I run the same command under two identical Python project on 2 PCs
python ./main -xls nd.xls -xml nd.xml -t 1234
PC A : works greate
PC B throws the exception
Traceback (most recent call last):
File "./main", line 21, in <module>
from color_print import *
ImportError: No module named color_print
from color_print import *
from debug_tool import *
The both python edition are 2.7
I just curious why works on PC A and failed on PC B.
I must make some mistakes.
.
├── common
│   ├── Common
│   │   ├── __init__.py
│   │   ├── color_print.py
│   │   └── debug_tool.py
│   ├── Excel
│   │   ├── Xls.pyc
│   │   ├── XlsOperation.py
│   │   ├── XlsOperation.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── README.md
│   ├── __init__.py
│   ├── __init__.pyc
│   └── tmpl.py
├── main
├── nd.xls
├── nd.xml
├── nd_excel.py
├── nd_excel.pyc
├── nd_xml.py
├── nd_xml.pyc
└── origin_nd.xls

Related

Unable to import functions in Python

Trying to resolve some packages >= Python 3.3
├── dags
│   ├── common
│   │   ├── aws.py
│   │   ├── conf.py
│   │   ├── constants.py
│   │   ├── metamorph.py
│   │   └── sensor.py
│   ├── global.cfg
│   ├── dag_1
│   │   ├── README.md
│   │   ├── configuration
│   │   │   └── config.json
│   │   ├── dag
│   │   │   ├──
│   │   │   └── test.py
in test.py I'm trying to import things from common, but unable to get it resolved. I've print things on the sys.path but not seeing any conf in there. What am I missing?

Django error with test module while running tests

When i run python manage.py tests i get an error saying that some test module is not found.
I am using PyCharm, Django 2.1.4 and W10 on Ubuntu.
The error:
======================================================================
ERROR: projectname.projectname (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: projectname.projectname
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/loader.py", line 462, in _find_test_path
package = self._get_module_from_name(name)
File "/usr/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
ModuleNotFoundError: No module named 'projectname.projectname'
What I've tried
python manage.py runserver and it runs just fine.
Add projectname to INSTALLED_APPS
Create and app called tests
My project structure
Django
│   ├── requirements.txt
│   └── projectname
│   ├── __init__.py
│   ├── manage.py
│   └── projectname
│   ├── apps
│   │   ├── accounts
│   │   │   ├── admin.py
│   │   │   ├── apps.py
│   │   │   ├── __init__.py
│   │   │   ├── migrations
│   │   │   │   ├── __init__.py
│   │   │   ├── models
│   │   │   │   ├── __init__.py
│   │   │   │   ├── profiles.py
│   │   │   │   └── users.py
│   │   │   ├── serializers
│   │   │   │   └── __init__.py
│   │   │   ├── tests.py
│   │   │   ├── urls.py
│   │   │   └── views
│   │   │   └── __init__.py
│   │   ├── __init__.py
│   ├── db.sqlite3
│   ├── __init__.py
│   ├── settings
│   │   ├── base.py
│   │   ├── development.py
│   │   ├── production.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   └── wsgi.py
I just want to run my tests like in any other django project...
I have never encountered this problem before so any help is appreciated! :)
Well, well...it turns out that changing the folder was the solutio, though i have projects working that share the same folder name so i don't really know what happend with this one.
Before:
Django
│ └── projectname
│ └── projectname
After:
Django
│ └── othername
│ └── projectname

Issue with turning a test file into a package with __init__.py

I am following along with this tutorial:
http://www.marinamele.com/taskbuster-django-tutorial/create-home-page-with-tdd-staticfiles-templates-settings
When I run:
(tb_test)metersk:functional_tests metersky$ python test_all_users.py
.
----------------------------------------------------------------------
Ran 1 test in 2.313s
OK
The test works as it should. The test being :
from selenium import webdriver
import unittest
class NewVisitorTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
def test_it_worked(self):
self.browser.get('http://localhost:8000')
self.assertIn('Welcome to Django', self.browser.title)
if __name__ == '__main__':
unittest.main(warnings='ignore')
However, when I follow the step to add a blank __init__.py file to the directory, and I run the test like so: python manage.py test functional_tests
I get a the following failure:
Traceback (most recent call last):
File "/Users/metersky/code/taskbuster_project/taskbuster/settings/base.py", line 20, in get_env_variable
return os.environ[var_name]
File "/Users/metersky/.virtualenvs/tb_test/bin/../lib/python3.4/os.py", line 633, in __getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
File "/Users/metersky/code/taskbuster_project/taskbuster/settings/__init__.py", line 2, in <module>
from .base import *
File "/Users/metersky/code/taskbuster_project/taskbuster/settings/base.py", line 31, in <module>
SECRET_KEY = get_env_variable('SECRET_KEY')
File "/Users/metersky/code/taskbuster_project/taskbuster/settings/base.py", line 23, in get_env_variable
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: set the SECRET_KEY environment variable
I don't quite understand how calling the test script differently from the command line would prevent the program from finding the secret key environment variable. It clearly works when I call the script on its own.
Folder structure:
├── TaskBuster.sublime-project
├── TaskBuster.sublime-workspace
├── db.sqlite3
├── functional_tests
│   ├── __init__.py
│   └── test_all_users.py
├── manage.py
├── requirements
│   ├── base.txt
│   ├── development.txt
│   ├── production.txt
│   └── testing.txt
└── taskbuster
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-34.pyc
│   ├── settings.cpython-34.pyc
│   ├── urls.cpython-34.pyc
│   └── wsgi.cpython-34.pyc
├── db.sqlite3
├── settings
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── base.cpython-34.pyc
│   │   └── development.cpython-34.pyc
│   ├── base.py
│   ├── development.py
│   ├── production.py
│   ├── staging.py
│   └── testing.py
├── static
│   ├── css
│   │   ├── bootstrap-theme.css
│   │   ├── bootstrap-theme.css.map
│   │   ├── bootstrap-theme.min.css
│   │   ├── bootstrap.css
│   │   ├── bootstrap.css.map
│   │   ├── bootstrap.min.css
│   │   └── main.css
│   ├── favicon.ico
│   ├── fonts
│   │   ├── glyphicons-halflings-regular.eot
│   │   ├── glyphicons-halflings-regular.svg
│   │   ├── glyphicons-halflings-regular.ttf
│   │   └── glyphicons-halflings-regular.woff
│   ├── img
│   └── js
│   ├── main.js
│   └── vendor
│   ├── bootstrap.js
│   ├── bootstrap.min.js
│   ├── jquery-1.11.2.min.js
│   ├── modernizr-2.8.3-respond-1.4.2.min.js
│   └── npm.js
├── templates
│   ├── 404.html
│   ├── base.html
│   ├── humans.txt
│   └── robots.txt
├── urls.py
└── wsgi.py

How to fix "ImportError: No module named ..."

I have reviewed most of the similar question here.
I'm new to python and I'm using Ubuntu 13.10
The project structure is
├── projecttest
│   ├── api.py
│   ├── controller
│   │   ├── controller.py
│   │   ├── controller.pyc
│   │   ├── init_db.py
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── settings.py
│   │   ├── settings.pyc
│   │   └── extra
│   │   ├── extra.py
│   │   ├── extra.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── __init__.py
│   ├── lib
│   │   └── __init__.py
│   ├── models
│   │   ├── documents.py
│   │   ├── documents.pyc
│   │   └── __init__.py
All the __init__.py files are empty (no hidden characters) and when I'm trying
$ python init_db.py
that has:
from projecttest.models.documents import *
I'm getting:
Traceback (most recent call last):
File "controllers/init_db.py", line 1, in <module>
from projecttest.models.documents import *
ImportError: No module named projecttest.models.documents
You need to specify PYTHONPATH environment variable, it augments the default search path for module files.
It helps to think about PYTHONPATH as an absolute path. If you specify it you may import modules within your program relative to PYTHONPATH.
In your case it would be something like the following line:
PYTHONPATH=/<dir>/<folder>/projecttest/ python init_db.py
Then you may import modules without problems like:
from models.documents import *

Python - how do i load my custom class methods from a root directory?

How do import my class from a specific directory (in my case root directory i want to keep it).
So, i have following directory map, now i need to load the class parsePresets from myglobal.py file, which is located in root directory: /var/tmp/mypython directory.
but, i want to import that from class/methods from my new module: /var/tmp/mypython/media/test.py with:
from myglobal import parsePresets
but i am getting:
from myglobal import parsePresets
ImportError: No module named myglobal
i also have init.py in root directory and in the media directory.
$ cd /var/tmp/mypython; tree
.
├── arduino
│   ├── arduino.diest.c
│   ├── arduino.gent.c
│   ├── arduino.lalouvier.c
│   ├── arduino.makenoise.c
│   ├── arduino.servo.c
│   ├── arduino.string.c
│   ├── arduino.tcpserver.c
│   ├── arduino.tcpserver.c~
│   ├── arduino.test.sh
├── bash
│   ├── all.sh
│   ├── alsa-info.sh
│   ├── asound.conf
│   ├── autoreboot.sh
│   ├── diskfix.sh
│   ├── kernelfix.sh
│   ├── update.sh
│   └── usbformat.sh
├── chrome.py
├── download.py
├── download.sh
├── gui.py
├── image
│   ├── a.png
│   ├── b.gif
│   ├── cross_new.png
│   ├── e150
│   │   ├── 1.png
│   │   ├── de.png
│   │   ├── en.png
│   │   ├── __init__.py
│   │   └── nl.png
│   ├── __init__.py
│   ├── logo.png
│   ├── menu.jpg
│   └── slider_btn.png
├── __init__.py
├── INSTALL
├── ip.py
├── loading.py
├── logout.py
├── media
│   ├── __init__.py
│   ├── test.py
├── menu.py
├── myglass.py
├── myglass.pyc
├── myglobal.py
├── myglobal.pyc
├── rightclick.py
├── runme.sh
├── server.py
├── server.pyc
├── src.nja
├── test
│   ├── Button.py
│   ├── json.py
│   ├── json.pyc
│   ├── keyboard.py
│   ├── loop.sh
│   ├── mytimer.py
│   ├── qtclick.py
│   ├── qtmouse.py
│   ├── qt.py
│   ├── qtwindows7.py
│   ├── shape.py
│   ├── skeleton.py
│   ├── slider.py
│   ├── testpreview.py
│   ├── test.py
│   ├── Text.py
│   ├── transparent.py
│   ├── transparentwindow.py
│   └── Vscale.py
├── test.py
├── unavailable.py
├── upload.sh
└── internet
├── backup
├── protocol.txt
└── server.py
You can add sys.path.append(/var/tmp/mypython/media/) to your script.
EDIT:
$ cat >> /var/tmp/mypython/stackoverflow.py <<\EOF
import sys
sys.path.append("/var/tmp/mypython/")
from myglobal import parsePresets
EOF
$ python /var/tmp/mypython/stackoverflow.py
or with NINJA-IDE
Running: /var/tmp/mypython/media/stackoverflow.py (Wed Dec 11 13:37:25 2013)
Execution Successful!
Use relative imports
from ..myglobal import parsePresets
The extra periods . take you "out" a level in your hierarchy.

Categories