Import local copy of python module - python

I have a basic question about how to package a python script with a local copy of a module, in this case the yaml module, so that i can run it on another machine and not have to download/install the module.
I tried taking the 'yaml' directory from the repo on github, putting it in the root of my python script, and simply running an import statement, but that doesn't seem to work; when i run import yaml or from yaml import load, my script fails with exception unexpected token '='.
I'm sure that I'm missing something obvious, but I wasn't able to find a clear answer by looking around for a bit. Any help would be appreciated.

Related

Importing self-made package

I am testing my own package, but I am struggling to import it. My package file structure is as follows:
(You can also alternatively view my Github repository here)
In PyAdventures is my init.py with the content of
name="pyadventures"
So my question is, when I import it in another python file, how come it doesn't work?
I run:
import pyadventures
But I get the following error:
No module named pyadventures
It'd be great if you could answer my question!
It's important to note that the package is in my Python package directory, not the test one I showed
New discovery! In my PyAdventures folder (the one Python actually uses) the folder only has a few files, not the ones in the screenshot above.
You can install this with pip install pyadventures
Ah, like others remark in comments and answer: The camelcase might be the problem. (Why not name it just all in lower case: pyadventures? - Else users will be as confused as its developer now :D .)
Before, I thought, it might be a problem that you want to use your module without having installed it (locally). And my following answer is for this case (using a not installed package from a local folder):
In the docs you can read:
The variable sys.path is a list of strings that determines the
interpreter’s search path for modules. It is initialized to a default
path taken from the environment variable PYTHONPATH, or from a
built-in default if PYTHONPATH is not set. You can modify it using
standard list operations:
import sys
sys.path.append('/ufs/guido/lib/python')
thus, before import do:
import sys
sys.path.append('/absolute/or/relative/path/to/your/module/pyadventures')
# Now, your module is "visible" for the module loader
import pyadventures
Alternatively, you could just place your pyadventures module folder locally in your working directory where you start python and then just do
import pyadventures
However, it is much easier to manage to keep only one version in one place and refer to this from other places/scripts. (Else you have multiple copies, and have difficulties to track changes on the multiple copies).
As far as I know, your __init__.py doesn't need to contain anything. It works if it is empty. It is there just to mark your directory as a module.
Simple. Even though the package listed on pip is namd pyadventures, in your code the directory is called PyAdventures. So that's what python knows it as. I ran import PyAdventures and it worked fine.

AttributeError: 'ModuleSpec' object has no attribute 'load_data_wrapper'

This is a bit long so bear with me.
I am trying to learn both Python and Linux and am very new to both. I am currently doing some reading on deep learning from the following:
http://neuralnetworksanddeeplearning.com/chap1.html
I am attempting to import the mnist_loader package to use the associated data for testing the script that was previously written. However, upon typing import mnist_loader into the Linux command line, I was given the following:
"the program 'import' can be found in the following packages:"
at which point it listed some packages. Because I'm new to Linux and I don't have admin privileges, I decided to go a route that I understood better; that is to create a new python script and simply use the import command within (which has worked in all previous attempts).
I created a python script and tried import mnist_loader and received the following error:
"ModuleNotFoundError: No module named 'mnist_loader'"
I then checked my C drive and found that the file was indeed there. Here is a link to the Git repository where the files may be found:
https://github.com/MichalDanielDobrzanski/DeepLearningPython35
Next I moved on to trying to directly input the path to the file as follows:
import importlib.util
mnist_loader = importlib.util.spec_from_file_location("mnist_loader",r"C:\Users\XXXXXX\Documents\neural-networks-and-deep-learning-master\neural-networks-and-deep-learning-master\src\mnist_loader.py")
training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
However, this produced the following error:
"AttributeError: 'ModuleSpec' object has no attribute 'load_data_wrapper'"
Note: the last line is used to collect the necessary data from the nist files.
I am running out of thoughts at this point and would love some feedback on all my "wrongdoings" up till now.
Thanks in advance!
P.S. It is worth noting that the book uses a package designed for Python 2.X whereas I am using 3.6. The readme provided by the book file location mentioned a different location where a Python 3.6 version could be found which is what I am going with.
Been a while since I have worked with Python, but I have some ideas as to what would cause the specific errors you are seeing. First I would suggest setting a PYTHON_PATH environment variable with the path you have that contains the module you want to import, this does not require administrator privileges fortunately. As for the load_data_wrapper attribute, you might have to do a from from mnist_loader import * to import all the functions inside the mnist_loader module.

Path for python is different than my .bash_profile

I'm working through Learn Python the Hard way, and I'm currently on exercise 46 where you learn to create packages. I've created a basic package that does a few calculations, and uses a few different modules.
I've gotten the package to install in my python2.7 site packages, but I can't seem to run the module from my site packages after the fact. I'm wondering if the path that python is searching is different, because of the following:
After the install, I see this message Copying story-0.1-py2.7.egg to /usr/local/lib/python2.7/site-packages
However, when I try to run the module, I see this message /usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'story.py': [Errno 2] No such file or directory
Sorry if this makes absolutely no sense, I'm very new to the fantastical world of programing.
When you install a package, you access the internals differently. You can no longer just call python story.py.
To access the functions in story.py you need to import the module at the top of another python file, or in the interpreter.
If story.py contained
def my_test_function(blah):
print blah
You would use this function in another file by the following (after installing the module, as you've already done)
import story
story.my_test_function("Hello!")
By importing the module, you get access to all the functions and classes inside it by typing module_name.function_name. You could also just import the function you wanted to use directly, which would look something like
from story import my_test_function
my_test_function("Hello!")

Libraries act differently depending on if they are installed or not

I have a Python library I wrote which has been acting up on me. I have a set of variables which change the way the library works. In testing it all worked fine, but when I python lib.py install the variables have no effect on the library. I broke this down to the simplest example possible:
Library:
##lib.py
config="Original"
def run():
print config
Script:
import lib
lib.config="New"
lib.run()
print lib.config
If you place the library in the same directory as the script and run it the output is:
New
New
But if you install the library and then try the script using the library from the dist-packages the output is:
Original
New
Could someone explain what is going on? I'm a bit confused and terribly interested in the happenings and reason. Additionally am I doing programatical configuration totally wrong?
Edit
It turns out the problem is the init.py file. It's basically like importing a library that just imports another library. When you import an installed module it looks at the folder lib and the file init.py. init.py is just a one liner from lib import *. It simply pretends to be the actual library, but that causes an odd problem if you use a global variable. A simulated example of what is essentially going on:
##init.py
from lib import *
Script:
import init
init.config = 'New'
init.run()
print init.config
Output:
Original
New
The function run() looks for config in lib.py, but print init.config looks for it in init.py. Thanks for the help everyone. Fix is to change the way the module installs (no init.py). Eventually, I hope to remove all the global variables, but for the time being everything works perfect.
What you describe would be inconsistent with how Python works (read, if you like, "I don't believe you did precisely that and got precisely that result").
If you go importing lib from different places or in different ways, though, you could be ending up with two copies of it, either two copies of one of the modules or one of the current-directory lib and the other the installed lib. If you are getting this "Original"/"New" behaviour, that seems to me the most likely reason.

Python ImportError (Cannot import name Variables)

I have done some research on this question and nothing seems to fix it, I suspect I may be accidentally circularly importing somewhere, but this one has stumped me.
This package is a Python IRC Service framework. The issue occurs when dynamically loading a module (via imp.load_source()), and the new module tries to import something from the src folder (Variables.py). It fails with "Cannot import name Variables", yet, it imports another module from the same directory without complaint.
Here is the specific error:
**ERROR: ModLoad(): Unable to load module
/home/sam/workspace/Affinity/bin/../modules/ircd_affinity.py:
cannot import name Variables
**
This is the full file for modules/ircd_affinity.py: ircd_affinity.py, and src/Variables.py: Variables.py
If it helps. browse the entire repository... this (https://github.com/miniCruzer/Affinity/blob/master/src/Affinity.py) module (starting at line 123) loads modules.
I would appreciate if all responses were kept relevant to the specific issue at hand. Yes, it may be tempting to suggest other things I'm doing wrong or otherwise throughout the entire package - I am welcome to suggestions: please e-mail them to me.
Show the full traceback. Look through it for evidence of (attempted) circular import.
Run Python with the -v option ... what does that tell you?
Look at the contents of sys.path and sys.modules just before the error happens.
"**ERROR: ModLoad(): Unable to ..." doesn't look line a Python-raised error. Is that in your code? If so, dig deeper; why is it raised?

Categories

Resources