Python 2.7: Odd importing behavior - python-2.7

Screenshot
I have:
C:/Python27/site-packages
in my Path (as shown in the screenshot.)
I also have the folder pywinauto within C:/Python27/site-packages, so I have
C:/Python27/site-packages/pywinauto
That folder contains some modules which I use. For some reason I can import pywinauto by typing:
import pywinauto
but it's contents are not imported unless I explicitly say:
from pywinauto import <module>
I think I have everything setup correctly, is there anything that is being overlooked?

Modules internal to a package are not automatically imported when you import the package. When you import a package, only its __init__.py gets executed. In many cases it contains nothing, but it's common to put there some definitions and other imports.
So, in pywinauto/__init__.py, include this line:
import application
Then, this should work:
import pywinauto
print pywinauto.application
The alternatives are to import like this:
import pywinauto.application
Or like this (as you already suggested):
from pywinauto import application

Related

webapp2 - how do I include library

I am testing Google Cloud and first I wanted to develop something on my PC before I use in on google cloud.
I am using APACHE and configured it in that way, that when I am going to the page localhost/wsgi_app I see my page which physically is stored in folder /svc/http/webapp2. File wsgi_app.py which contains my app is stored in subfolder webapp2. All works fine. I provide this information just in case it might play any role.
The issue I have is with import from library.
I did it on Django and now try to move it to webapp2.
The first lines of my program look like this:
import webapp2
import MySQLdb
import json
I have file called test.py which contains some classess and funtions.
it is in the same folder as wsgi_app.py.
I want to include it, however this seems not to work:
import webapp2
import MySQLdb
import json
from test import *
my test.py contains definition of the class 'Quote', but when I call the page I see error
NameError: global name 'Quote' is not defined
When I put the inside of the file test.py in the file wsgi_app.py all works fine.
My goal is to separate the code into several files.
Any idea why :
from test import *
does not work ?
It worked on any other program I wrote, so why not here?
issue solved.
issue is not related to weapp2 but to WSGI and the path where python search for files.
Most simple solution is to add something like this ;
execfile("/srv/http/test.py")
it will import the file.
other than this here is quite good article
http://blog.dscpl.com.au/2014/09/python-module-search-path-and-modwsgi.html

Custom import package Python

I am making a import package and I keep running into a problem when I try and import and use the package. Primarily when I try to run it.
import mypackage
once I import it I can use it I dont get any errors upon importing it, however when I go to use it I get an error that says
TypeError: 'module' object is not callable
In a folder that I have named myfolder which is located in my current working directory i have a empty init.py file as well as two other .py files which contain the functions i have one called test.py and another called testing.py.... my problem isnt importing them but when I go to use them I get the TypeError.
the folders structure is as follows,
current working directory
-Mypackage
-__init__.py
-test.py
-testing.py
I import it like this,
from Mypackage import test
I use it like this,
test(stuff)
You have to learn how to use modules in python. When you import mypackage into your script it imports a module which has two scripts in it. Let's say they are first.py and second.py . Now if you use something like
from mypackage import first
And then you use a function named func like this
first.func()
Then it should work.
In short either import all the functions from your module or use dot notation to reference these functions. More on module can be read here

Import local module in python

I just change to new company and they use python to dev. When I see the source from ancestors, I see they use sys.path to import local module:
import sys, os
sys.path.append(os.path.dirname(os.path.realpath(__file__))+"/folder/")
from folder import module as module
I think it is not right. I suggest them to use package like this:
import path.to.module as module
They ignore my suggest.
Am I right? I google but I don't find any hint for this.
Your method will only work if the module is in your path. Python will add your current working directory to sys.path so it should work for local modules.
However, if you are trying to import a module from another directory, you will likely need to use sys.path.append first
There is an other way is by setting a path to environment variable "PYTHONPATH"

PyCharm/Django - cannot import module views.py (IDE is wrong)

I'm having a weird problem with import views in Django project. I'm not sure whether it is a problem caused by PyCharm of Django. So PyCharm says that it can't import views.py file, but it works when I run the server.
Here is the picture:
Do you know where could be the problem?
EDIT:
According to Inlangers answer, I've tried to change import to from vlado_web.translations import views which did not helped, moreover, it raises
Exception Value:
No module named translations
When I have from translations import views there, it works correctly but PyCharm says that it can't be resolved.
PyCharm doesn't know where your source files are. Try this:
Right click on folder vlado_web (the folder that contains manage.py) within PyCharm. Go to Mark Directory As and choose Sources Root.
This will let PyCharm know that the vlado_web directory is the root folder for your source code, and will allow you to perform absolute imports from there, e.g.
from vlado_web.translations import views
Try from vlado_web.translations import views

Python 2.7 packages and importing between directories

Ok so I've been struggling with this for over a week now and I've tried various methods mentioned on this site and others on google but here goes. I'm running python 2.7. I've got a python script in the parent directory which calls a second script located in a child directory. The second script starts a different thread and does an os.system call to a third and final script. This third script needs to import something located in the parent directory. Can someone tell me what's wrong with this setup? I do have __init__.py located in every folder being used. And I do try adding the relative parent directory to the path. I'm not sure where I'm going wrong.
File Structure
Parent Directory
Python_Script_1.py
Imports_needed.py
__init__.py
Child Directory
Python_Script_2.py
Python_Script_3.py
__init__.py
Method 1
Python_Script_2.py
import os
import sys
import multiprocessing
def listen():
listen_string = "python ~/path/Python_Script_3.py"
os.system(listen_string)
q = multiprocessing.Process(target=listen())
Python_Script_3.py
import sys
sys.path.append("..")
import Imports_needed
ImportError: No module named "Imports_needed"
Method 2
Python_Script_2.py
import os
import sys
import multiprocessing
def listen():
listen_string = "python ~/path/Python_Script_3.py"
os.system(listen_string)
q = multiprocessing.Process(target=listen())
Python_Script_3.py
import sys
sys.path.append("..")
from .. import Imports_needed
ValueError: Attempted relative import in non-package
Question about method 2, how come it's telling me this is not a package despite each directory containing __init__.py?
Additionally, I've used something very similar to method 1 in the past but I cannot see any differences between my code or file structures. If anyone has any suggestions I'd greatly appreciate it. Thanks.
EDIT: Apologies, I forgot something fairly significant in the description. There's a third file involved that I completely forgot about... So sorry :( Python_Script_2 makes a new thread via multiprocessing and does an os.system call to Python_Script_3. Python_Script_3 is located in the same child directory as Python_Script_2. Python_Script_3 is having the import issues when trying to import Imports_needed.py from the Parent Directory. I've updated the question to reflect this. (I'm sorry, I know this is a major detail that I left out but it's a complicated package and doing many more things than just what I'm asking about)
try the following
from Parent_Directory import Imports_needed
or
from Parent_Directory.Imports_needed import <your class or method>
You don't need
sys.path.append("..")
at all. If Python_Script_2.py is imported from the main script, import path inside of Python_Script_2.py will be relative to the main script. So
import Imports_needed
is enough.
Make sure you hava double underscore prefix for the file _init__.py in your child directory (in your example you have only 1 underscore symbol _).
UPDATE
If you rely on sys.path.append(), you should use:
import os
import sys
sys.path.append(os.path.realpath('..'))
otherwise, '..' says nothing to the interpreter about your app environment.