Custom import package Python - python-2.7

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

Related

pytest make sure your test modules/packages have valid Python names -- can't import

I have this file structure.
When ı wrote pytest in my terminal.
ı have this issue !
ModuleNotFoundError: No module named 'money_transactions_test'
Hint: make sure your test modules/packages have valid Python names.
Why its can't import money_transactions_test ?
If you're sharing objects within your tests the easiest thing would be to make use of conftest.py
However if you must import, since your test files are in the same package directory I recommend specifying a relative import. ie:
from .money_transactions_test import acc_numbers_list

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

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"

python import module from a parent directory

I have the following structure and I am using the below to import functions
( i.e basically importing a module from a parent directory)
from demo import sayHello
It works but in Pycharm it says unresolved reference. Should I continue or should I use the relative path like below, then I get no warnings
from ..demo import sayHello
Let me know the correct way of importing
app
demo.py
__init__.py
**Controllers**
AccountController.py
__init__.py
Yes, in python it's best to use the relative path within the project. (Based on the question, I can't tell where you're trying to use the imported function, but I'm assuming it's within the same project)
Packages like os, sys etc are built within your Python executable, so those are available globally. Anything else needs to be relative.

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.