Installing a module on Choregraphe - pepper

I am trying to install a python module to use in Choregraphe. For windows this does not appear to be an issue. All I have to do is make sure that the module is installed in Python on the operating system.
For OSX, it does not appear to recognise the module. I have also tried importing it as a folder to a project, but it still can't see it.

Do you try playing with the python system path: the location when he looks for library.
Like that (in your choregraphe box):
import sys
sys.path.append("path containing your_module folder")
import your_module

From the linked site:
1. Download python scripts of Requests and its dependencies from github.
2. Copy these scripts into my Project content(I made 'lib' directory in the project and copied all scripts into the folder)
3. Add the following code to import the modules
import sys, os
framemanager = ALProxy("ALFrameManager")
folderName = os.path.join(framemanager.getBehaviorPath(self.behaviorId), "../lib")
if folderName not in sys.path:
sys.path.append(folderName)
import requests
4. Add the following to unload the modules
import sys
if self.folderName and folderName in sys.path:
sys.path.remove(folderName)

Related

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"

Trouble importing shared object in Python

I am attempting to import a shared object into my python code, like so:
import bz2
to which I get the following error:
ImportError: ./bz2.so: cannot open shared object file: No such file or
directory
Using the imp module, I can verify that Python can actually find it:
>>> import imp
>>> imp.find_module('bz2')
(<open file 'bz2.so', mode 'rb' at 0xb6f085f8>, 'bz2.so', ('.so', 'rb', 3))
The shared object file is in my PYTHONPATH and my LD_LIBRARY_PATH.
Any insights into why I can't import this shared object? Thanks!
bz2.so is the shared object the provides the bzip functionality (which was written in C) for the python modules. You don't import it directly when you do import bz2 , you are actually importing a python module called bz2 which then uses the .so file.
This usually means you haven't got the development version of the bzip library installed or you don't have a c compiler setup for the pip installer to use to build this for you.
You don't say which linux you are using but the general pattern is look in the package manager for bzip2 dev or devel packages and install those.

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

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.

Python27 zipimport does not recognize .zip file

I need to import modules such as numpy as zip files from non-standard directories.
I try to import numpy from the full path C:\Users\Anders\Downloads\numpy-1.8.1.zip\ which is downloaded from here: http://sourceforge.net/projects/numpy/files/NumPy/1.8.1/numpy-1.8.1.zip/download
Script:
import zipimport
zipimport.zipimporter("C:\Users\Anders\Downloads\numpy-1.8.1.zip")
import numpy
Output:
zipimport.zipimporter("C:\Users\Anders\Downloads\numpy-1.8.1.zip")
ZipImportError: not a Zip file
What the heck is that about?
I've had similar problems with zipimport unable to parse the full path in Window environments.
The fix I follow is to change directories into the folder containing the zip file.
import zipimport
import os
os.chdir("C:\Users\Anders\Downloads")
zipimport.zipimporter("numpy-1.8.1.zip")
Then, I specify a relative path to the zip file. I tested this locally for the case you mentioned without any complications.