I have several scripts in a project folder, most of which use the same handful of standard libraries and modules. Instead of having to reiterate in every single script
import pandas as pd
import numpy as np
import datetime
import re
etc
etc
is it possible for me to place all import statements in a masterImports.py file and simply import masterImports at the top of each script ?
Yes you can.
So the basic idea is to import all the libraries in one file. Then import that file.
An example:
masterImports.py
import pandas as pd
import numpy as np
import datetime
import re
etc
etc
otherFile.py
import masterImports as mi
print(mi.datetime.datetime(2021,7,20))
Or you could use wildcard imports -
from masterImports import * # OR from masterImports import important_package
print(datetime.datetime(2021,7,20))
Do not use wildcard asterix imports because there can be name clashes
Try this, and you will see that there is no error
It's possible, although not really the done thing
To use it, you'd need to do, at the top of each script:
from master_imports import *
Related
import os
import geopandas as gpd
import pandas as pd
file=os.listdir(r\\IP_Address\winshare\Affected_Area_Files)
path=[os.path.join(r\\IP_Address\winshare\Affected_Area_Files,i)
for i in file:
if".shp" in i:
# Below code need to be explained:
gdf=gpd.GeoDataFame(pd.concat([gpd.read_file(i).to_crs(gpd.read_file(path[0]).crs)for i in path],ignore_index=True),crs=gpd.read_file(path[0]).crs)
gdf.to_file(r\\IP_Address\winshare\affected_area\combined-AffectedArea_Test.shp)
I am new to python, the code has to be optimized for better performance.
It was running fine but now it is taking quite long to execute
I am trying to pull the file "house_date.csv" and I ma being unssucesful becasue python is stating that the file cannot be found. is there a better way for me to figure out how I can load the file ? I am also not sure in what directory is located.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import math
pd.read_csv('house_data.csv')
This is the error message I am getting .
When you write pd.read_csv('house_data.csv'), the csv file directory and the python working directory is supposed to be the same.
Try to replace house_data.csv with the complete path (for example C:/house_data.csv, if the file is in "C").
There are several imports that are common between some files in my project. I would like to reuse this code, concentrating it in a unique file and have just one import in the other files. Is it possible?
Or is there another way not to replicate the desired import list in multiple files?
Yes its possible. You can create a Python file with imports and then import that Python file in your code.
For Eg:
ImportFile.py
import pandas as pd
import numpy as np
import os
MainCode.py:
from ImportFile import *
#Here you can use pd,np,os and complete your code
OR
from ImportFile import pd,np
#And then use pd and np
I am taking this code from "Modeling Techniques in Predictive Analytics" by Thomas Miller:
from __future__ import division, print_function
from future_builtins import ascii, filter, hex, map, oct, zip
import pandas as pd
from pandas.tools.rplot import RPlot, TrellisGrid, GeomPoint, ScaleRandomColour
And getting the error:
No module named rplot
This code worked at one time. How can I tell if rplot is in pandas or not? Is that why I am getting this error? Could it be removed?
I have the following two files:
/quiz/quiz/views.py
and
/quiz/static/questions.html
I would like to import questions.html into views.py.
So I'm trying to do this:
from ..static import *
However, I'm getting:
ValueError: Attempted relative import beyond toplevel package
As Sebastian Wozny answered, you can not import such files in python.
Try to write module that parse useful data from html file then import that module.