Importerror:No module named in python - python-2.7

I created a file named hello.py in /home/Desktop/problems directory
and i want to import it to a file present in /home/Desktop/PP
#hello.py file
def matrix():
print "enter into matrix"
and i want to access this hello.py file in a file named new.py
so i did like this..
#new.py file
import sys
import os
sys.path.append(os.path.abspath("/home/Desktop/problems"))
from hello import *
matrix()
But i am getting an error like no module named new
Thanks in advance.
UPDATE: The directory order was wrong..so it was giving the error.. it should have been /home/valac/Desktop/problems !!

I created both files with the content described here and I ran your new.py:
python new.py
And it worked just fine. How are you executing or using your new.py script? The problem is probably related to that. Can you explain how are you using new.py?

Do you have init file in the pp folder? You may create init.py under folder pp.
The init.py does not need to contain any code. It will change the folder as a module.

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

Python: Can't import a function from another.py file

I have a file named handshake.py. Where there is a function send_data(argument). I want to import that function into another file named siptest.py. I am encountering two problems. I am using microsoft visual studio with windows 7, 64-bit.
1) I can't import function. I have tried using,
from handshake import*
handshkae.send_data(argument)
Which give me an error.
NameError: global name 'handshake' is not defined
Another option I have tried is using
import handshake
handshake.send_data(argument)
Which gives me an attribute error.
AttributeError: 'module' object has no attribute 'send_data'
If I use it the other way, such as
from handshake import send_data
2) MS Visual studio says. No test discovered, please check the configuration settings but I still can run the test somehow. and it says that the test is failed because of Import Error.
ImportError: cannot import name send_data
Both of the said files are in same directory. Plus the function is defined in a class 'TCPhandshake' in handshake.py
One possible reason: there exists reference cycle between module a.py and b.py:
In a.py: import b
In b.py: import a
The solution is to break the cycle. You need to make it clear which module should do what and reduce the dependence.
Make sure both files are in the same directory and try:
from handshake import send_data
If this does not work, try to rename handshake.py file.
Are both handshake.py and siptest.py in the same directory?
If not You can try this:
1) Add __init__.py empty file to the directory that contains handshake.py.
2) Then add the path to that directory to LD_LIBRARY_PATH and PYTHONPATH
make sure the function is in the path
import sys
sys.path.insert(0, '/directory/tothe/handshakefile/')
and then
import handshake
I had the same issue, That happened while I tried to run the program from another directory by using python /home/name/workspace/test.py
Fix I tired.
import sys
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
This need to be added at the beginning.
This works for me.
I find a way to import files in the same directory by implementing keyword as and a object variable for example
import file as fileObj
But the downside is that when you want access to the imported files variable you have to first take fileObj.fileObjVariable.
Try adding/updating the environment variable PYTHONPATH which should point to the folder that has handshake.py
This is very simple, but for me it worked to restart the kernel.
Just make sure the files all are located in the root of the project, then this will work:
import handshake
handshake.send_data(argument)

Can't import sqldf in one python file while it works in another. Both files are in different folders. How can I set this right?

In one python file when I try from pandasql import sqldf it works. The path for that is
C:\Users\AmitSingh\Anaconda2\python.exe "C:/Users/AmitSingh/PycharmProjects/HelloPython.py/exercise 2"
In another file when I use the same command it gives me the error
ImportError: cannot import name sqldf
The path for this file is
C:\Users\AmitSingh\Anaconda2\python.exe C:/Users/AmitSingh/Desktop/Data/Udacity/Intro_Machine_Learning/ud120-projects/datasets_questions/explore_enron_data.py
I don't understand why? When I write import sqldf the prompt shows sqldf as an autocomplete option. But doesn't work.
By default, you can't. When importing a file, Python only searches the current directory, the directory that the entry-point script is running from, and sys.path which includes locations such as the package installation directory (it's actually a little more complex than this, but this covers most cases).
However, you can add to the Python path at runtime:
# some_file.py
import sys
sys.path.insert(0, '/path/to/application/app/folder')
import file

Using imp.load_source() throws "No module named .."

I am using imp module to load a python file(exists at: /parent_folder/path/to/my_module/my_module.py) from source:
mod = imp.load_source("my_module", "/parent_folder/path/to/my_module/")
However, my_module.py file is also importing other modules written and saved in a same folder location:
my_module.py
....
...
from other_module import other_thing
...
The load_source fails complaining about No module named other_module. What would be the best way to load the file which handles all the imports? I would prefer achieving this by using python import library function rather than toying with the sys.path
Your path needs to include the full pathname to the file, including the '.py' at the end:
mod = imp.load_source("my_module", "/parent_folder/path/to/my_module/my_module.py")
For me, explicitly importing the other dependency worked.
imp.load_source('path/to/other/module/other_module.py')
Try putting it before loading my_module.

Can't import module from library

I have a script that requires two other modules to be loaded from another folder in the same directory as the script.
In the main script I have the line:
from modules import *
But I keep getting this error:
AttributeError: 'module' object has no attribute 'ts3'
I have an __init__.py file in the modules folder which has this line:
__all__ = ['setjoin', 'ts3']
Python is for some reason unable to load just the ts3 module. It is able to load setjoin completely fine.
Folder structure:
MAOW\
maow.py
\modules\
__init__.py
setjoin.py
ts3.py
Any help would be much appreciated, thanks :)
I've found the problem. I'm not sure why I did this, but I was trying to import maow from ts3.py. I've now removed that import and it works fine :)