I'm learning Python and the imports that have accumulated whilst developing my app seem like I'm repeating imports. Please can someone advise me on development practices.
from pathlib import Path
from urllib.request import urlopen
from gi.repository import Gtk, Gio
from gi.repository import GLib as glib
from gtk_assistant import AssistantApp
import urllib.request
import urllib.error
import xml.etree.ElementTree as ET
import json
import gi
import sys
import os
import hashlib
You can group imports like this if you want:
from urllib import request, error
from xml.etree import ElementTree as ET
import json, gi, sys, os, hashlib
However, the pep8 style guide says you should have them on seperate lines so the way you did it is fine
Related
I am trying to implement the code in https://github.com/kexinyi/ns-vqa.
However, when I try the command, python tools/preprocess_questions.py \ ... in the section of "Getting started". I see a message No module named 'utils.programs'.
Then I install utils and which makes import utils work, but import utils.programs does not work.
Does anyone have any idea to solve it?
import os
import argparse
import json
import h5py
import numpy as np
import utils.programs as program_utils # this one cannot be imported
import utils.preprocess as preprocess_utils
import utils.utils as utils
Solution:
Add the below lines at the beginning of preprocess_questions.py file.
import sys
sys.path.insert(0, "..")
This should solve your problem.
Explanation:
It is failing because preprocess_questions.py don't know about the path of utils.programs to import. With the above lines added to the path using .., the required file will be imported.
For more on this refer how importing works in python.
When i do this imports and i create a exe file with pyinstaller. The dist folder is huge (+900Mbs). How can i reduce this?
import queue
import threading
import time
import dateutil.parser
import getpass
import json
import pandas as pd
import pickle
import pprint
import requests
import sys
import urllib3
import xlrd as xl
import xlwings as xw
import PySimpleGUI as sg
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from pathlib import Path
from openpyxl import load_workbook, cell
from pandas import ExcelWriter
from datetime import datetime, date
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
I got this code in Python 2.7:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import logging
import math
import operator
import pickle
import re
import sys
import threading
import time
import unicodedata
from random import shuffle
import currencylayer
import quandl
import requests
from GUI import *
from arduinoSerial import *
from bs4 import BeautifulSoup
from ledDisplay import *
from timeout import timeout
When I run it an error occurs:
from GUI import *
ImportError: No module named GUI
I try to install GUI, but I couldnt:
Try to run this command from the system terminal. Make sure that you use the correct version of 'pip' installed for your Python interpreter located at '/home/in/PycharmProjects/untitled/venv/bin/python
Did I made somehting bad? Could someone help me?
Is there a module called "GUI" that you're trying to import?
Tkinter is the standard Python GUI framework. Maybe you're thinking of that, in which case you would need to use from tkinter import *.
I want to monitor if the file has been changed through using watchdog in Python, however, when I test, the terminal give me a warning,
"C:\Python27\lib\site-packages\watchdog\observers\__init__.py:89:UserWarning:Failed to import read_directory_changes. Fall back to polling.warning.warn("Failed to import read_directory_changes. Fall back to polling")"
I've installed pip to install watchdog, however, I still don't know how this problem occur.
here is the test code:
# coding=utf-8
import sys
import time
import logging
import urllib
import urllib2
import os
# import MySQLdb
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
print "hello"
I've two Python scripts as given below
inner.py
#!/usr/bin/python
import os
import datetime
# <---- Some Code--->
main.py
#!/usr/bin/python
import os
import datetime
# <---- Some Code--->
subprocess.call(["/usr/bin/python",inner.py])
The problem is when the inner.py script is called from the main.py script it doesn't import any modules. For example it says
ImportError: No module named os
But when the script is executed standalone it works fine. Please help
The following works perfectly fine for me, and it's modified because some of your code seemed a little incomplete.
inner.py
#!/usr/bin/python
import os
import datetime
print os.getcwd()
main.py
#!/usr/bin/python
import os
import datetime
import subprocess
import sys
# <---- Some Code--->
subprocess.call([sys.executable, "inner.py"])