python-import

python: from x import y changes previous import result

大城市里の小女人 提交于 2019-12-23 20:26:58
问题 I am trying to understand the package and module name shadowing rules in python and stumbled across a case where I do not understand why the results that I see make any sense. This cases happens for python 2 (with from future import absolute_imports ) and python 3. Assuming I have the following folder structure: ├── mypackage │ ├── argparse.py │ └── __init__.py └── script.py mypackage is my custom top-level package, where I have a module shadowing the standard argparse module. Inside my

How can I `import *` from a module loaded with imp?

不问归期 提交于 2019-12-23 18:55:46
问题 I currently work with keras and custom activation functions. I store those models as pickle and I would like to be able to load them again. Then I run into this issue. The problem is that the Python script which contains the custom activation function is given by path. I load this script via import imp model_module = imp.load_source('model', experiment_meta['model']['script_path']) How can I "star import" ( import * ) in this case to make the loading of the trained model work? What I've tried

Python 3 isinstance unexpected behavior when importing class from different file?

。_饼干妹妹 提交于 2019-12-23 15:38:39
问题 I am trying to import a class from one file and check if is an instance of that class in the file that it was defined in. The problem is that instead of returning True from the isinstance() function, it returns False , because it was initialised in a different file. Here is a working example. Say you have file1.py : class Foo: def __init__(self, arg1): self.arg1 = arg1 def main(class_obj): # Prints false and is type <class 'file1.Foo'> print(type(class_obj)) print(isinstance(class_obj, Foo))

Difference between import and execfile

橙三吉。 提交于 2019-12-23 09:45:02
问题 I have a file utils.py containing a function called f1() . From another Python script I can import utils or execfile('utils.py') and have access to f1() . What are the differences between the two methods? 回答1: There are many differences, but from your point of view the most significant is probably that import gives you more control over the namespace in which the objects defined in utils.py end up. Let's consider three variants on import . The first is the one you asked about: import utils

Difference between import and __import__ in Python

本秂侑毒 提交于 2019-12-23 09:39:36
问题 I was taking a look at some commit of a project, and I see the following change in a file: - import dataFile + dataFile = __import__(dataFile) The coder replaced import dataFile by dataFile = __import__(dataFile) . What exactly is the difference between them? 回答1: import dataFile translates roughly to dataFile = __import__('dataFile') Apparently the developer decided that they wanted to use strings to identify the modules they wanted to import. This is presumably so they could dynamically

If I have Pandas installed correctly, why won't my import statement recognize it?

∥☆過路亽.° 提交于 2019-12-23 04:48:06
问题 I'm working on a project to play around with a csv file, however, I can't get pandas to work. Everything I have researched so far has just told me to make sure that pandas is installed. Using pip I have managed to install pandas just fine. When I try to run my python program and import pandas I get: Traceback (most recent call last): File "analysis.py", line 1, in <module> import pandas as pd ImportError: No module named pandas I don't know if this is related, but I was trying to see see if

Missing lxml module in python?

↘锁芯ラ 提交于 2019-12-22 17:58:36
问题 I want o use Python-docx library to process word files. A docx.py references lxml, as i assume from from lxml import etree When i start the script, i get error: No module named lxml Is this a standard library? Why is not it referenced properly then? IronPython 2.7 RC1. 回答1: You need to install lxml which is not part of the stdlib. I don't know if it will work with IronPython though. Update : Seems like it might be non-trivial to get lxml working with IronPython. See this question: How to get

Select a module using importlib and use in multiprocessing functions

限于喜欢 提交于 2019-12-22 17:48:34
问题 I'd like to select in my main function which module to import based on an argument passed to the Python script. So, I'm using one of blah = importlib.import_module("blah1") blah = importlib.import_module("blah2") where the 'blahX' are different implementations of the same interface. I also want to pass the work off to different processes using the multiprocessing module. blah = None def f(a, b): print blah.f(a,b) if __name__ == '__main__': # call importlib.import_module here... a = 1 b = 2 p

Python generates SystemError when trying to import a module into another module

╄→гoц情女王★ 提交于 2019-12-22 14:03:19
问题 I have a file calc.py where I have methods for basic calculations.Now I create another file (in the same directory) called test_calc.py for unittesting the methods in calc.py file.But when I try to run test_calc.py either through command line using python3 -m unittest test_calc.py or since I have included name == " main " python3 test_calc.py or try to run it directly through the IDE,I get an error that says from . import calc SystemError: Parent module '' not loaded, cannot perform relative

Python imports are very slow - Anaconda python 2.7

主宰稳场 提交于 2019-12-22 11:00:54
问题 My python import statements have become extremely slow. I'm running python 2.7 locally using the Anaconda package. After importing modules, the code I wrote runs very quickly, it just seems to be the imports that take forever. As an example, I ran a "tester.py" file with the follow code: import timeit x = timeit.timeit('import numpy as np') print 'imported numpy in %s seconds'%x x = timeit.timeit('import pandas as pd') print 'imported pandas in %s seconds'%x x = timeit.timeit('from Tkinter