Python27 zipimport does not recognize .zip file - python-2.7

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.

Related

Import package within sub-package [duplicate]

Assume I have the following files,
pkg/
pkg/__init__.py
pkg/main.py # import string
pkg/string.py # print("Package's string module imported")
Now, if I run main.py, it says "Package's string module imported".
This makes sense and it works as per this statement in this link:
"it will first look in the package's directory"
Assume I modified the file structure slightly (added a core directory):
pkg/
pkg/__init__.py
plg/core/__init__.py
pkg/core/main.py # import string
pkg/string.py # print("Package's string module imported")
Now, if I run python core/main.py, it loads the built-in string module.
In the second case too, if it has to comply with the statement "it will first look in the package's directory" shouldn't it load the local string.py because pkg is the "package directory"?
My sense of the term "package directory" is specifically the root folder of a collection of folders with __init__.py. So in this case, pkg is the "package directory". It is applicable to main.py and also files in sub- directories like core/main.py because it is part of this "package".
Is this technically correct?
PS: What follows after # in the code snippet is the actual content of the file (with no leading spaces).
Packages are directories with a __init__.py file, yes, and are loaded as a module when found on the module search path. So pkg is only a package that you can import and treat as a package if the parent directory is on the module search path.
But by running the pkg/core/main.py file as a script, Python added the pkg/core directory to the module search path, not the parent directory of pkg. You do have a __init__.py file on your module search path now, but that's not what defines a package. You merely have a __main__ module, there is no package relationship to anything else, and you can't rely on implicit relative imports.
You have three options:
Do not run files inside packages as scripts. Put a script file outside of your package, and have that import your package as needed. You could put it next to the pkg directory, or make sure the pkg directory is first installed into a directory already on the module search path, or by having your script calculate the right path to add to sys.path.
Use the -m command line switch to run a module as if it is a script. If you use python -m pkg.core Python will look for a __main__.py file and run that as a script. The -m switch will add the current working directory to your module search path, so you can use that command when you are in the right working directory and everything will work. Or have your package installed in a directory already on the module search path.
Have your script add the right directory to the module search path (based on os.path.absolute(__file__) to get a path to the current file). Take into account that your script is always named __main__, and importing pkg.core.main would add a second, independent module object; you'd have two separate namespaces.
I also strongly advice against using implicit relative imports. You can easily mask top-level modules and packages by adding a nested package or module with the same name. pkg/time.py would be found before the standard-library time module if you tried to use import time inside the pkg package. Instead, use the Python 3 model of explicit relative module references; add from __future__ import absolute_import to all your files, and then use from . import <name> to be explicit as to where your module is being imported from.

ValueError: Attempted relative import in non-package in pyspark/not able to find the kmodes module(pyspark)

ValueError: Attempted relative import in non-package in pyspark. Above error is coming when i am trying to use kmode in pyspark .So i parrelelize the kmode package by using
sc.addFile("home/pyspark-distributedkmodesmaster/pyspark_kmodes/pyspark_kmodes.py") .If i am not using the sc context add file code it is throwing error -"not able to find the kmodes module".
I am using below code/link or kmodes pyspark package -
"https://github.com/ThinkBigAnalytics/pyspark-distributed-kmodes/blob/master/pyspark_kmodes"
It's a package and you're only deploying one of the py files contained in it. Since there is a relative import in pyspark_kmodes.py:
from .Kmodes import KModes
The . in .Kmodes means it should look for file KModes.py in the same folder (within the package folder)

Installing a module on Choregraphe

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)

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

import python packages from a zip file in c/c++ standalone application

am using a python scripts in c/c++ program and i want to import python packages from a zip file as in Sublime Text, is there a better way to do it?
Use zipimport.
>>> import sys
>>> sys.path.insert(0, 'example.zip') # Add .zip file to front of path
>>> import foobar