python-import

Disk Defrag and Disk Clean up using Python Script

北城以北 提交于 2019-12-25 04:26:19
问题 Can you help me on how to make this script work. For Defrag import os; defragmentation=os.popen('defrag.exe /C').read() print(defragmentation); For Disk Clean up import os; clean=os.popen('cleanmgr.exe /sagerun:1').read() print(clean); Upon trying this scripts, it didnt do anything and no error message prompt. Thank you. 回答1: If your defrag.exe or cleanmgr.exe are not in your path , they won't execute and you won't get an error message You would need to run the scripts as an administrator to

Suppress warnings on import?

你离开我真会死。 提交于 2019-12-25 04:15:34
问题 Assuming I write a python package that has to use the imp module, and my package is "TestModule" which is the following: import imp import pip import sys def update_and_reload(module, *args, **kwargs): pip.main(['install', module, '--upgrade', '--user']) return imp.reload(module) When I do import TestModule in the terminal, I get a pending deprecation warning on imp . How would I make imp 's warning not occur / filter out? 回答1: Well you could use the warning module: import warnings with

Creating a socket in python

泪湿孤枕 提交于 2019-12-25 04:08:24
问题 import socket import sys HOST = '' # Symbolic name meaning all available interfaces PORT = 8888 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print 'Socket created' try: s.bind((HOST, PORT)) except socket.error , msg: print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] sys.exit() print 'Socket bind complete' s.listen(10) print 'Socket now listening' #wait to accept a connection - blocking call conn, addr = s.accept() #display client

Creating a socket in python

末鹿安然 提交于 2019-12-25 04:08:03
问题 import socket import sys HOST = '' # Symbolic name meaning all available interfaces PORT = 8888 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print 'Socket created' try: s.bind((HOST, PORT)) except socket.error , msg: print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] sys.exit() print 'Socket bind complete' s.listen(10) print 'Socket now listening' #wait to accept a connection - blocking call conn, addr = s.accept() #display client

Reloading a function within a module

寵の児 提交于 2019-12-25 01:58:59
问题 Goal I would like to import a custom module from the interpreter, run it, modify it, reload it, and run it again to see the changes. Background I'm using python 2.7. Before I started writing my own modules, I made great use of the reload() function in python 2.7. I reproduced my issue in a simple example I create a folder called: demo Inside demo , I place two files: __init__.py and plotme.py My __init__.py file contains: from .plotme import plot My plotme.py file contains: import matplotlib

Raspberry pi No module named 'cx_Oracle'

痴心易碎 提交于 2019-12-24 23:22:57
问题 I want to use the raspberry pi to send values to the Oracle11g database, but when I run import cx_Oracle syntax for that process, I get the following error: Traceback (most recent call last): File "/home/pi/20190222ex01.py", line 1, in <module> import cx_Oracle File "/usr/lib/python3/dist-packages/thonny/backend.py", line 317, in _custom_import module = self._original_import(*args, **kw) ImportError: No module named 'cx_Oracle' How can I solve this problem? 回答1: There is no ARM port of the

How to import package sub-modules in Python and avoid accidentally duplicating the module?

别等时光非礼了梦想. 提交于 2019-12-24 21:53:09
问题 I have a package with submodules, and I want to ensure I don't duplicate the module (e.g. import it with different absolute paths and accidentally have the module symbols be duplicated). For example: my_package/ __init__.py my_submodule.py I want to be able to import my_package.my_submodule globally and something like import my_submodule from within the package. How do I do this? EDIT: This is effectively a duplicate of this thread: Relative imports for the billionth time However, I've

How import a function from another view with Django?

假如想象 提交于 2019-12-24 19:24:13
问题 I have this folder hierarchy: |---- saga |---- core |---- views.py |---- study_time |---- views.py On my study_time/views.py , I have this functions: def study_time(request): def tasks_subjects(week_day, key): #Code here return __tasks def day_studies(week_day): __tasks_subjects = tasks_subjects(week_day, 0) #Code here return __studies return render(request, 'study_time.html', context) On my core/views.py , I need the day_studies() function, so I'm importing like this: from saga.study_time

Importing another project as modules in python

你。 提交于 2019-12-24 17:28:30
问题 Suppose I have a project in the following structure projectfoo/ |- mymodule/ |--|- __init__.py |--|- library.py |- preprocessor.py and in the __init__.py in mymodule looks like this from . import library #library itself has other functions def some_function(): blar blar blar... and the preprocessor.py would look like follows import mymodule def main(): something() def something(): mymodule.some_function() # calls the function defined in __init__.py if __name__ == '__main__': main() Then I

import werkzeug VS from werkzeug import security

半城伤御伤魂 提交于 2019-12-24 13:27:41
问题 My current understanding (based on these answers: one, two, three; and Python documentation) of how import in Python works is (just in case it matters: all the code snippets are tested on Python 3.6.1) : Say we have a module mod , which has submodules sub and sub1 ; sub , in turn, has a function func ; then we can (given that mod installed in current environment, of course): import mod mod.sub.func() mod.sub1 # or import mod.sub mod.sub.func() mod.sub1 # will result in "NameError: name 'mod'