Using one module inside another in Python - python

I am trying to make some code a bit more modular in Python and am running into one issue which I'm sure is straight forward but I can't seem to see what the problem is.
Suppose I have a script, say MyScript.py:
import pandas as pd
import myFunction as mF
data_frame = mF.data_imp()
print(data_frame)
where myFunction.py contains the following:
def data_imp():
return pd.read_table('myFile.txt', header = None, names = ['column'])
Running MyScript.py in the command line yields the following error:
Traceback (most recent call last):
File "MyScript.py", line 5, in <module>
data_frame = mF.data_imp()
File "/Users/tomack/Documents/python/StackQpd/myFunction.py", line 2, in data_imp
return pd.read_table('myFile.txt', header = None, names = ['column'])
NameError: name 'pd' is not defined

You need to import pandas in your function or script myFunction:
def data_imp():
import pandas as pd
return pd.read_table('myFile.txt', header = None, names = ['column'])

Answers here are right, because your module indeed lacks myFunction import.
If stated more broad this question can also contain following: in case of circular import the only 2 remedies are:
import pandas as pd , but not from pandas import something
Use imports right there in functions you need in-placd

Related

I can not figure out why I can not start this simlple python script

My directory looks like this
When I start directly with PyCharm it works.
But when I try to start the script with a commandline I get this error messsage
> python .\PossibilitiesPlotter.py
Traceback (most recent call last):
File "C:\Users\username\PycharmProjects\SwapMatrixPlotter\possibilitiesplotter\PossibilitiesPlotter.py", line 7, in <module>
from plotterresources.PlotterProps import PlotterProps
ModuleNotFoundError: No module named 'plotterresources'
This is how the import looks from my main class PossibilitesPlotter.py
import sys
sys.path.append("plotterresources/PlotterProps.py")
from csv import reader
from pathlib import Path
from plotterresources.PlotterProps import PlotterProps
from possibilitiesplotter.PossibilitiesGraph import PossibilitiesGraph
from possibilitiesplotter.PossibilitiesModel import PossibilitiesModel
class PossibilitiesPlotter:
As a workaround, add the following line to PossibilitesPlotter.py:
sys.path.append("../plotterresources/PlotterProps.py")
This will add the directory one level above the commandline pwd to the PATH variable. So this is always relative to the location of the calling script/shell.
Thus in general:
NEVER append to the PATH/PYTHONPATH variable from within modules. Instead restructure your module. For more details, take a look at the documentation on Packaging Python Projects

Can I import a list of modules from a shared file? i.e. can I import imports?

I have about a dozen python module imports that are going to be reused on many different scrapers, and I would love to just throw them into a single file (scraper_functions.py) that also contains a bunch of functions, like this:
import smtplib
import requests
import re
from urllib.request import urlopen
from bs4 import BeautifulSoup
import time
def function_name(var1)
# function code here
then in my scraper I would simply do something like:
import scraper_functions
and be done with it. But listing the imports at the top of scraper_functions.py doesn't work, and neither does putting all the imports in a function. In each case I get errors in the scraper that is doing the importing.
Traceback (most recent call last):
File "{actual-scraper-name-here}.py", line 24, in <module>
x = requests.get(main_url)
NameError: name 'requests' is not defined
In addition, in VSCode, under Problems, I get errors like
Undefined variable 'requests' pylint(undefined-variable) [24,5]
None of the modules are recognized. I have made sure that all files are in the same directory.
Is such a thing possible please?
You need to either use the scraper_functions prefix (same way you do this import name) or use the from keyword to import your things from scraper_functions with the * selector.
Using the form keyword (Recommended)
from scraper_functions import * # import everything with *
...
x = requests.get(main_url)
Using the scraper_functions prefix (Not recommended)
import scraper_functions
...
x = scraper_functions.requests.get(main_url)

Pandas in VS Code: AttributeError: module 'string' has no attribute 'ascii_letters'

I'm a new learner in data science. I havent find out why I got an attribute error. I used python 3.8.3 in Visual Studio Code. I installed Pandas in terminal (pip install Pandas). I dont know what the problem is. Any help will be appreciated.
import pandas as pd
df=pd.DataFrame()
print(df)
All I did was to create an empty dataframe. And I got that:
Traceback (most recent call last):
File "c:/Users/Fatma Elik/Documents/VS Code/BTK/Pandas_dataframe.py", line 20, in <module>
import pandas as pd
File "C:\Users\Fatma Elik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\__init__.py", line 180, in <module>
import pandas.testing
File "C:\Users\Fatma Elik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\testing.py", line 5, in <module>
from pandas._testing import (
File "C:\Users\Fatma Elik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\_testing.py", line 404, in <module>
RANDS_CHARS = np.array(list(string.ascii_letters + string.digits), dtype=(np.str_, 1))
AttributeError: module 'string' has no attribute 'ascii_letters'
Secondly I tried this instead and I got an attribute error again:
import pandas as pd
s1=pd.Series([3,2,0,1])
s2=pd.Series([0,3,7,2])
data=dict(apples=s1,oranges=s2)
df=pd.DataFrame(data)
print(df)
I did Ctrl+Click on string and I find that I already created a py file before. Because I searched on file search engine Windows 10 before, I couldnt have found it. Another simple mistake again :)
It's a little weird, the "string" module is a part of the standard library. Could you try this code?
from string import ascii_letters
print(ascii_letters)
Check it works or not, if it doesn't work, can you enter into this file:
"C:\Users\Fatma Elik\AppData\Local\Programs\Python\Python38-32\lib\string.py", and can you find:
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_letters = ascii_lowercase + ascii_uppercase
It should be located from line 25 to 27. If you can't find it, you should try to upgrade your python or reinstall it.

pd not defined when importing custom functions locally

I have files with functions saved locally that I want to load. However, when I do this, its not picking up already loaded loaded packages such as pandas.
import pandas as pd
import numpy as np
x=pd.DataFrame({"a":[1,2,3]})
This function is saved locally to a test_function.py file...it isn't run in my jupter notebook directly.
# saved on file locally...
def multiply_values(data):
if data.__class__ == pd.DataFrame():
return(x.iloc[:,0]*2)
Load the function...
from test_function import *
multiply_values(x)
I then get
NameError: name 'pd' is not defined
Can someone explain to me how I should be handling this? How am I not importing this correctly?
I also updated the test_function.py file to have
import pandas as pd as the very first line & within the function itself.
I get this response:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-75-00666e9e2407> in <module>
1 xx = pd.DataFrame({'a':[0,23,234,3]})
2
----> 3 multiply_values(xx)
~/Downloads/testfunc.py in multiply_values(data)
1 import pandas as pd
2
----> 3
4 def multiply_values(data):
5 import pandas as pd
NameError: name 'pd' is not defined

ImportError with Python 3

I am trying to run an example from ThinkStats2 code. I copied everything from github - the file structure are exactly the same as given on the github.
chap01soln.py
from __future__ import print_function
import numpy as np
import sys
import nsfg
import thinkstats2
def ReadFemResp(dct_file='2002FemResp.dct',
dat_file='2002FemResp.dat.gz',
nrows=None):
"""Reads the NSFG respondent data.
dct_file: string file name
dat_file: string file name
returns: DataFrame
"""
dct = thinkstats2.ReadStataDct(dct_file)
df = dct.ReadFixedWidth(dat_file, compression='gzip', nrows=nrows)
CleanFemResp(df)
return df
def CleanFemResp(df):
"""Recodes variables from the respondent frame.
df: DataFrame
"""
pass
def ValidatePregnum(resp):
"""Validate pregnum in the respondent file.
resp: respondent DataFrame
"""
# read the pregnancy frame
preg = nsfg.ReadFemPreg()
# make the map from caseid to list of pregnancy indices
preg_map = nsfg.MakePregMap(preg)
# iterate through the respondent pregnum series
for index, pregnum in resp.pregnum.iteritems():
caseid = resp.caseid[index]
indices = preg_map[caseid]
# check that pregnum from the respondent file equals
# the number of records in the pregnancy file
if len(indices) != pregnum:
print(caseid, len(indices), pregnum)
return False
return True
def main(script):
"""Tests the functions in this module.
script: string script name
"""
resp = ReadFemResp()
assert(len(resp) == 7643)
assert(resp.pregnum.value_counts()[1] == 1267)
assert(ValidatePregnum(resp))
print('%s: All tests passed.' % script)
if __name__ == '__main__':
main(*sys.argv)
I am getting ImportError like shown below:
Traceback (most recent call last):
File "C:\wamp\www\ThinkStats_py3\chap01soln.py", line 13, in <module>
import nsfg
File "C:\wamp\www\ThinkStats_py3\nsfg.py", line 14, in <module>
import thinkstats2
File "C:\wamp\www\ThinkStats_py3\thinkstats2.py", line 34, in <module>
import thinkplot
File "C:\wamp\www\ThinkStats_py3\thinkplot.py", line 11, in <module>
import matplotlib
File "C:\Python34\lib\site-packages\matplotlib\__init__.py", line 105, in <module>
import six
ImportError: No module named 'six'
All the files are listed under the src folder. No packages under src. I tried adding packages for nsfg and thinkstats but another error appeared. I tried upgrading python 2.7 to python 3.4. I am still getting the same error. I know I need to install the six package for matplotlib but why am I getting import error on nsfg?
You are getting the import error on nsfg because it internally imports matplotlib (not directly, but it imports thinkstats2 , which imports thinkplot which imports matplotlib , which has a dependency on six module) . And that library is not installed on your computer, so the import fails.
Most probably you do not have six module , you can try installing it using - pip install six .
Or get it from here, unzip it and install it using - python setup.py install

Categories

Resources