python-import

Importing modules: __main__ vs import as module

*爱你&永不变心* 提交于 2020-01-18 04:43:16
问题 To preface, I think I may have figured out how to get this code working (based on Changing module variables after import), but my question is really about why the following behavior occurs so I can understand what to not do in the future. I have three files. The first is mod1.py: # mod1.py import mod2 var1A = None def func1A(): global var1 var1 = 'A' mod2.func2() def func1B(): global var1 print var1 if __name__ == '__main__': func1A() Next I have mod2.py: # mod2.py import mod1 def func2():

How to catch an ImportError non-recursively? (dynamic import)

空扰寡人 提交于 2020-01-17 04:23:30
问题 Say we want to import a script dynamically, i.e. the name is constructed at runtime. I use this to detect plugin scripts for some program, so the script may not be there and the import may fail. from importlib import import_module # ... subpackage_name = 'some' + dynamic() + 'string' try: subpackage = import_module(subpackage_name) except ImportError: print('No script found') How can we make sure to only catch the possible import failure of the plugin script itself, and not of the imports

Is there a way to import a python package from another python package, as if it was part of the same code?

五迷三道 提交于 2020-01-16 14:01:47
问题 Let's say that we have python packages package_a and package_b . As a user, I want to be able to say: from package_a.package_b import some_functionality My question is: is there a way to support this without literally copy-pasting the code of package_b to package_a ? EDIT 1: To clarify, I have shadow naming (my python package is named the same way as my folder), and I am wondering if there was a way to make the python package available somewhere else. And yes, I'm looking into ways how to

Importing a variable from another script and keeping it updated

只谈情不闲聊 提交于 2020-01-16 03:18:22
问题 I have two .py scripts. script1.py and script2.py I am importing few variables from script2.py like: from script2 import variable1 as var1 which works fine. But when I update variable1 in script2.py , and then re-run script1.py , the update of variable1 doesn't show up in script1.py . Why is that so? The update of variable1 shows up if I close IPython completely and then re-open IPython again. But I don't want to do this all the time as I need few plot's to be open. I am using IPython 1.2.1

create Python package and import modules

扶醉桌前 提交于 2020-01-15 11:16:50
问题 I'm trying to write my first Python package, and almost all my modules will need to use NumPy. Should I write import numpy in every single module or is there some place in the package I can just import it once so every module can use it? What's the best way to do this? 回答1: Yes, just import it everywhere it's needed. Don't get too clever with writing functions that import everything for you or metamodules that import things and from which you import * ; all of that only serves to make your

Using builtin __import__() in normal cases

余生长醉 提交于 2020-01-15 09:36:30
问题 Here is where I checked the performance of __import__() In [9]: %%timeit ...: math = __import__('math') ...: sqrt = math.sqrt ...: sqrt(7894561230) ...: The slowest run took 11.16 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 534 ns per loop In [10]: %%timeit ...: from math import sqrt ...: sqrt(7894561230) ...: ...: The slowest run took 10.23 times longer than the fastest. This could mean that an intermediate result is

Using builtin __import__() in normal cases

断了今生、忘了曾经 提交于 2020-01-15 09:35:07
问题 Here is where I checked the performance of __import__() In [9]: %%timeit ...: math = __import__('math') ...: sqrt = math.sqrt ...: sqrt(7894561230) ...: The slowest run took 11.16 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 534 ns per loop In [10]: %%timeit ...: from math import sqrt ...: sqrt(7894561230) ...: ...: The slowest run took 10.23 times longer than the fastest. This could mean that an intermediate result is

Deleting Windows Temp Files using python script

你离开我真会死。 提交于 2020-01-15 09:28:29
问题 Can you help me how can i delete all the files under the Windows/Temp files?? Below are my scripts but it doesn't work at all. import os import subprocess recPath = 'C:\\Windows\\Temp' ls = [] if os.path.exists(recPath): for i in os.listdir(recPath): ls.append(os.path.join(recPath, i)) else: print 'Please provide valid path!' paths = ' '.join(ls) pObj = subprocess.Popen('rmdir C:\\Windows\\Temp\\*.* /s /q *.*'+paths, shell=True, stdout = subprocess.PIPE, stderr= subprocess.PIPE) rTup = pObj

Existing Django project deployment using Apache and Mod_wsgi (windows)

核能气质少年 提交于 2020-01-15 07:22:14
问题 I have a django project which I took it from github. I would like to run the project on my local using apache server. I have installed apache and want to run the project. But I am always getting this error. raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e)) ImportError: Could not import settings 'core.settings' (Is it on sys.path?): No module named cms.models Please find the configuration I am using. Python 2.7 Django 1.4 database in

Existing Django project deployment using Apache and Mod_wsgi (windows)

£可爱£侵袭症+ 提交于 2020-01-15 07:21:34
问题 I have a django project which I took it from github. I would like to run the project on my local using apache server. I have installed apache and want to run the project. But I am always getting this error. raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e)) ImportError: Could not import settings 'core.settings' (Is it on sys.path?): No module named cms.models Please find the configuration I am using. Python 2.7 Django 1.4 database in