python import from relative paths - python-2.7

Question about setting up a relative path import. I have a bunch of files (modules) that live in
'/Users/myname/Desktop/programX_files/programX/common/
such as:
/Users/myname/Desktop/programX_files/programX/common/constants.py
/Users/myname/Desktop/programX_files/programX/common/util/misc.py
Each of those modules has a line to import other modules from within the common/ directory as needed. Examples:
"constants.py" in (/Users/myname/Desktop/programX_files/programX/common/constants.py) contains the line:
import programX.common.util.misc as util_misc
And "misc.py" in (/Users/myname/Desktop/programX_files/programX/common/util/misc.py) contains the line:
import programX.common.constants as constants
Now I want to use those modules. How do I properly let python know to set the correct path dependencies so that it will know to look into (/Users/myname/Desktop/programX_files/programX/common/) and the subdirectories inside common?
I tried appending the path, but it does not work:
>>> import sys
>>> sys.path.append('/Users/myname/Desktop/programX_files/programX/')
>>> import programX.common.constants.py as constants
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named programX.common.constants.py
>>>
>>> sys.path.append('/Users/myname/Desktop/programX_files/programX/common/')
>>> sys.path.append('/Users/myname/Desktop/programX_files/programX/common/util')
>>>
>>> import programX.common.constants.py as constants
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named programX.common.constants.py
>>>
>>> import os
>>> os.getcwd()
'/Users/myname'
>>>
>>> import Desktop.programX_files.programX.common.constants.py as constants
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Desktop.programX_files.programX.common.constants.py
>>>
I'm guessing it might be some relatively simple solution, but I can't figure it out. Thanks for the answers.

Your code should look like this:
import sys
# set path to folder
sys.path.append('/Users/myname/Desktop/programX_files/')
# now import files
import programX.common.constants.py as constants

Related

AWS boto error message ImportError: cannot import name key

>>> import boto
>>> c = boto.connect_s3()
>>> b=c.get_bucket('beaubeaubeau')
>>> from boto.s3.key import key
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name key
I am new to boto
Any idea why I am getting this error?
I think you want:
from boto.s3.key import Key
boto.s3.key is a Python module and Key is a class contained in that module.

python error when importing from __init__ file

I have a package installed in my site-packages dir. Folder structure looks like this
MyPkg\
__init__.py
LogUtils\
__init__.py
logwrapper.py
Shortcuts\
__init__.py <-----this references LogUtils
somefile.py
When I do help ('modules') I see MyPkg listed. But I get the following error in IDLE:
>>> import MyPkg
>>> from MyPkg import LogUtils
>>> from MyPkg import Shortcuts
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
from MyPkg import Shortcuts
File "C:\Python27\lib\site-packages\MyPkg\Shortcuts\__init__.py", line 1, in <module>
from GoToUrl import go_to_url
File "C:\Python27\lib\site-packages\MyPkg\Shortcuts\GoToUrl.py", line 1, in <module>
from LogUtils import logger, log
ImportError: No module named LogUtils
Why would LogUtils import fine standing alone, but throw an error when being imported via an init file??
Seems to me you are lacking some backslashes
MyPkg\
__init__.py
LogUtils\
__init__.py, \
logwrapper.py
Shortcuts\
__init__.py, \
somefile.py
As you see yourself, you are not importing the same module:
>>> from MyPkg import LogUtils
vs.
from LogUtils import logger, log
The first imports a package called MyPkg.LogUtils, the second imports a package called LogUtils. Whether they exist or not depends on your python paths, but in general, if the first one works, change the second one to
from MyPkg.LogUtils import logger, log

Cannot use scipy.stats

I get an errr when using scipy.stats. in a script after importing scipy.
AttributeError: 'module' object has no attribute 'stats'
Within script editor I can click on stats after typing scipy. from the pulldown menu,
within python console I can not select python.stats from the pulldown menu, it's not there.
I'm using pandas 2.7 and SciPy 0.13.0
Why is that?
Any known issues?
expanding on my comment (to have a listed answer).
Scipy, as many other large packages, doesn't import all modules automatically. If we want to use the subpackages of scipy, then we need to import them directly.
However, some scipy subpackages load other scipy subpackages, so for example importing scipy.stats also imports a large number of the other packages. But I never rely on this to have the subpackage available in the namespace.
In many packages that use scipy, the preferred pattern is to import the subpackages to have them available by their names, for example:
>>> from scipy import stats, optimize, interpolate
>>> import scipy
>>> scipy.stats
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'stats'
>>> scipy.optimize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'optimize'
>>> import scipy.stats
>>> scipy.optimize
<module 'scipy.optimize' from 'C:\Python26\lib\site-packages\scipy\optimize\__init__.pyc'>
This is expected. Most of the subpackages are not imported when you just do import scipy. There are a lot of them, with a lot of heavy extension modules that take time to load. You should always explicitly import the subpackages that you want to use.
https://github.com/scipy/scipy/issues/13618
if you import scipy alone like this:
import scipy
then you use:
scipy.stats
You will get:
AttributeError: module 'scipy' has no attribute 'stats'
You have to import like this:
import scipy.stats
or
import scipy
import stats

ImproperlyConfigured: Error importing middleware app.middleware: "cannot import name get_host"

I've just upgraded to Django 1.5, and when I try and access a page, I get the following error:
ImproperlyConfigured: Error importing middleware app.middleware: "cannot import name get_host"
In the shell, I try a similar thing:
>>> from app import middleware
Traceback (most recent call last):
File "<console>", line 1, in <module>
File ".../middleware.py", line 2, in <module>
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect, get_host
ImportError: cannot import name get_host
It's failing on the import of get_host. It doesn't seem that this is deprecated, what's going on?
In Django 1.5 django.http.get_host() is replaced with request.get_host(). See the methods on HttpRequest object here.

Django 1.4 not recognizing my app

I am using django 1.4, and the app.module name does not seem to be working as expected for me. My settings.py has the app included as "survey.surveys".
Here is the django shell dump for an import I attempted through a preexisting file. The error says No module named ....
from survey.surveys.views.db import mobile_survey1
Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/pjain/dev/survey/survey/surveys/views/db.py", line 6, in <module>
from survey.surveys.models import Survey, Section, Question, Option
File "/home/pjain/dev/survey/survey/surveys/views/survey.py", line 7, in <module>
from survey.surveys.models import Survey, Section, Question, Option
ImportError: No module named surveys.models
However, when I copied and pasted the exact import which breaks, it imports fine in the shell.
from survey.surveys.models import Survey, Section, Question, Option
How can I fix this import in my project?