python-import

Error while importing module app using from Flask.app import db

痴心易碎 提交于 2020-03-04 18:56:26
问题 I'm new to flask, and have problem with importing db while running from python console/cmd. I'm facing this error : In[67]: os.getcwd() Out[67]: 'C:\\Users\\Desktop\\Python\\Flask' In[68]: os.listdir() Out[68]: ['app', 'app.db', 'config.py', 'Flask.py', 'migrations', 'venv', '__init__.py', '__pycache__'] In[69]: from Flask.app.models import User,Post Traceback (most recent call last): File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line

ModuleNotFoundError when using importlib.import_module

那年仲夏 提交于 2020-03-03 05:03:27
问题 I have the following folder structure and I have a test method in util.py. When the util method is run, I see an error with a module that is imported within the module where I am trying to get all classes. Parent --report <dir> ----__init__.py ----AReport.py ----names_list.py --util.py util.py import inspect import importlib import importlib.util def get_class_names(fileName): for name, cls in inspect.getmembers(importlib.import_module(fileName, package='report'), inspect.isclass): print(name

Import csv file as a list of tuples

牧云@^-^@ 提交于 2020-02-26 04:14:32
问题 I have a csv file containing tuples Test.csv "(1,2,3)","(1,2,4)","(1,2,5)" I want to import Test.csv as a list of tuples . Expected Result new_list = [(1,2,3),(1,2,4),(1,2,5)] Code Attempt import csv with open ('Test.csv', newline='') as file: reader = csv.reader(file) list_a = list(reader) new_list = [tuple(map(str,i)) for i in list_a] print(new_list) Current Output comes as list of strings ['(1,2,3)','(1,2,4)','(1,2,5)'] What is wrong with my approach? How can I solve this by not using ast

Import csv file as a list of tuples

北城以北 提交于 2020-02-26 04:13:05
问题 I have a csv file containing tuples Test.csv "(1,2,3)","(1,2,4)","(1,2,5)" I want to import Test.csv as a list of tuples . Expected Result new_list = [(1,2,3),(1,2,4),(1,2,5)] Code Attempt import csv with open ('Test.csv', newline='') as file: reader = csv.reader(file) list_a = list(reader) new_list = [tuple(map(str,i)) for i in list_a] print(new_list) Current Output comes as list of strings ['(1,2,3)','(1,2,4)','(1,2,5)'] What is wrong with my approach? How can I solve this by not using ast

How to check if a module is imported

大城市里の小女人 提交于 2020-02-25 03:58:07
问题 I use numpy and scipy for data analysis. I want to write a code inside a function that I define so that when the function is called it check if, for example, numpy is imported and if it is not imported, then it should import it. How can I check if any module such as numpy imported? 回答1: The import statement is idempotent - it already checks whether the module has been loaded. Using import module in your function already does what you want: def my_func(): import numpy # load numpy if not

Comparison: import statement vs __import__ function

我的未来我决定 提交于 2020-02-01 01:40:08
问题 As a followup to the question Using builtin __import__() in normal cases, I lead a few tests, and came across surprising results. I am here comparing the execution time of a classical import statement, and a call to the __import__ built-in function. For this purpose, I use the following script in interactive mode: import timeit def test(module): t1 = timeit.timeit("import {}".format(module)) t2 = timeit.timeit("{0} = __import__('{0}')".format(module)) print("import statement: ", t1) print("_

Aptana Python stdlib issue with virtualenv

橙三吉。 提交于 2020-01-30 10:35:04
问题 I recently started working on a project using just vim as my text editor with a virtualenv setup. I installed a few API's on this virtualenv from GitHub. Eventually, the project got a little bigger than vim could handle so I had to move the project to an IDE. I chose Aptana Studio 3. When I started up Aptana, I pointed the project directory to the virtualenv folder that I had created to house my project. I then pointed the interpreter at the Python executable in App/bin (created from

Aptana Python stdlib issue with virtualenv

不羁的心 提交于 2020-01-30 10:34:25
问题 I recently started working on a project using just vim as my text editor with a virtualenv setup. I installed a few API's on this virtualenv from GitHub. Eventually, the project got a little bigger than vim could handle so I had to move the project to an IDE. I chose Aptana Studio 3. When I started up Aptana, I pointed the project directory to the virtualenv folder that I had created to house my project. I then pointed the interpreter at the Python executable in App/bin (created from

Import urllib or urllib 2 in Python 2.7 failing with ImportError: cannot import name iskeyword

与世无争的帅哥 提交于 2020-01-29 23:17:26
问题 I can't import urllib or urllib2 on my Python 2.6 or Python 2.7 installs. Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import urllib Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/urllib.py", line 30, in <module> from urlparse import urljoin as basejoin File "/usr/lib/python2.7/urlparse.py", line 110, in <module> from collections import

Import urllib or urllib 2 in Python 2.7 failing with ImportError: cannot import name iskeyword

陌路散爱 提交于 2020-01-29 23:14:51
问题 I can't import urllib or urllib2 on my Python 2.6 or Python 2.7 installs. Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import urllib Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/urllib.py", line 30, in <module> from urlparse import urljoin as basejoin File "/usr/lib/python2.7/urlparse.py", line 110, in <module> from collections import