Refering to Files and Functions from within a Python package. - python

Let us have a look at the Directory structure of my Python Package
packman
weights.py
functions:
weigh()
force()
relatives.py
functions:
roll()
torque()
__init__.py
data
work.txt
rastor.txt
Now I have two questions.
Firstly suppose I want to access work.txt, from the function weigh() inside weights.py how would I address it?
I initially tried with this
f = open("data/work.txt")
While this method does succesfully work when the code is run inside main. However it fails to find the file when it is used as a package and it raises the issue
FileNotFoundError: [Errno 2] No such file or directory: 'data/work.txt'
How should I write the address of work.txt to make it more universal?
My other question is when I want to call the function weigh() of weights.py from the function roll() inside relatives.py, how would I do it?

I usually have a main.py or similar single entry point for my applications. Then I can do something like this to get the path for my application:
import os
app_location = os.path.dirname(os.path.abspath(__file__))
Now you can pass this location to your other modules or perhaps even use the same idea in them to get their location. Since you now have this location, you can easily do something like this:
data_location = os.path.join(app_location, 'data', 'work.txt')
with open(data_location) as f:
# do something with the file object
for line in f:
print(line)
As for your second question, just import weights into your relatives.py script and call weights.weigh()

Related

How to call function fron other python projects into anonther project stored in same directory?

I have 10 different python projects stored in one folder (F:\Python_Code...). I want to call user define functions from 10 different projects into the last project (Say Project11) and by running Project11, all my 10 projects should run one by one.
I have tried multiple ways like os.path() and from project1 import function, etc. but no one work. I read about the change in PYTHONPATH, but I am still not able to do that. I am using PyCharm. Can anyone help me to solve the problem?
soni smit!
Your solution wasn't that far away.
First you have to import the whole file with:
from . import filename
or just
import filename
if the file is in the same directory as your main file.
then you can call a function from that file with:
filename.functionname(arg1, arg2, ...)
I hope, it works for you!
~ostue
It's not a good idea to reference an upper-level directory for importing your packages.
If you're sure of what you're doing, you can change the working directory using os.chdir(path_to_dir_that_can_access_all_your_modules).
If you need the flexibility to import your libs in a dynamic way, try using importlib.import_module('module_name').
ex.:
import os, importlib
def import_module(base_path, module_path):
try:
backup_wd = os.getcwd() # backup original working directory
os.chdir(base_path) # change directory
return importlib.import_module(module_path) # import and return your module
except:
# Handle problems
...
finally:
os.chdir(backup) # go back to original directory in any case
project10_module = import_module('F:\Python_Code', 'project10.utils.yourmodule')
module_instance = project10_module(args)

Python Calling Function from Another File

I am using Python 2.7 on Windows 7 Professional.
I am trying to call a function saved in another file to run in this file's code.
Function called dosomething is found in anotherfile.py
anotherfile.py is in the same directory as current code.
My call in this file is simple:
import anotherfile
print anotherfile.dosomething
I am getting an error: No module named anotherfile
The problem is the same as I found in this post
I don't understand the solution but I'd like any insight?
Thank you.
EDIT: The other question/answers discuss resetting CLASSPATH and setting PYTHONPATH. I explored this but was not sure how to do this. Perhaps relevant?
Let us have two files in the same directory. Files are called main.py and another.py.
First write a method in another.py:
def do_something():
return "This is the do something method"
Then call the method from main.py. Here is the main.py:
import another
print another.do_something()
Run main.py and you will get output like this:
This is the do something method
N.B.: The above code is being executed using Python 2.7 in Windows 10.
Specify the module then the file then the import like so:
from this_module.anotherfile import dosomething
or if you want all functions from "anotherfile.py"
from this_module.anotherfile import *
and then you can call the "dosomething" command without the "anotherfile" prefix.
I ran into same problem. After ample of trials, I ended up solving it with the below mentioned solution:
Make sure your current file and anotherfile.py lies in same location of system path.
Say your another.py and current file lies at location : "C:/Users/ABC"
In case, one is not aware of system path. Use below code in current file:
import sys
print(sys.path)
import sys
sys.path.append('/C:/Users/ABC/')
Then you do below code in same current code:
from another import dosomething
I found the issue. Python was looking in another directory for the files. I explicitly set my working directory as the path to where thisfile.py and anotherfile.py reside and it works. Thank you for all the quick replies.

Making a directory for a file

I was making a exercise generator algorithm for my friend, but I stumbled across a problem. It is a python program, and I wanted to generate a folder in a directory that was above the program's location (like, the python file is in 'C:\Documents\foo' and the folder should be created in 'C:\Documents') so that it could then store the file the program created. Is there a way to do this or should I try something else?
Use the path argument of the os.mkdir() function.
Getting the current script directory is not a built-in feature, but there are multiple hacks suggested here.
Once you get the current script directory, you can build a path based off of that.
Not super familiar with Python in a Windows environment, but this should be easily do-able. Here is a similar question that might be worth looking at: How to check if a directory exists and create it if necessary?
Looks like the pathlib module might do what you are looking for.
from pathlib import Path
path = Path("/my/directory/filename.txt")
try:
if not path.parent.exists():
path.parent.mkdir(parents=True)
except OSError:
# handle error; you can also catch specific errors like
# FileExistsError and so on.
Appears to work on Win 7 with Python 2.7.8 as described:
import os.path
createDir = '\\'.join((os.path.abspath(os.path.join(os.getcwd(), os.pardir)), 'Foo'))
if not os.path.exists(createDir):
os.makedirs(createDir)

Creating a function to load data into a list? Passing filename to import through function

I am new to writing functions and modules, and I need some help creating a function within my new module that will make my currently repetitive process of loading data much more efficient.
I would like this function to reside in a larger overall module with other function that I can keep stored on my home directory and not have to copy into my working directory every time I want to call one of my function.
The data that I have is just some JSON Twitter data from the streaming API, and I would like to use a function to load the data (list of dicts) into a list that I can access after the function runs by using something like data = my_module.my_function('file.json').
I've created a folder in my home directory for my python modules and I have two files in that directory: __init__.py and my_module.py.
I've also went ahead and added the python module folder to sys.path by using sys.path.append('C:\python')
Within python module folder, the file __init__.py has nothing in it, it's just an empty file.
Do I need to put anything in the __init__.py file?
my_module.py has the following code:
import json
def my_function(parameter1):
tweets = []
for line in open(parameter1):
try:
tweets.append(json.loads(line))
except:
pass
I would like to cal the function as such:
import my_module
data = my_module.my_function('tweets.json')
What else do I need to do to create this function to make loading my data more efficient?
To import the module from the package, for example:
import my_package.my_module as my_module
would do what you want. In this case it's fine to leave the init.py empty, and the module will be found just by it being in the package folder "my_package". There are many alternatives on how to define a package/module structure and how to import them, I encourage you to read up, as otherwise you will get confused at some point.
I would like this function to reside in a larger overall module with
other function that I can keep stored on my home directory and not
have to copy into my working directory every time I want to call one
of my function.
For this to work, you need to create a .pth file in C:\Python\site-packages\ (or wherever you have installed Python). This is a simple text file, and inside it you'd put the path to your module:
C:/Users/You/Some/Path/
Call it custom.pth and make sure its in the site-packages directory, otherwise it won't work.
I've also went ahead and added the python module folder to sys.path by
using sys.path.append('C:\python')
You don't need to do this. The site-packages directory is checked by default for modules.
In order to use your function as you intend, you need to make sure:
The file is called my_module.py
It is in the directory you added to the .pth file as explained earlier
That's it, nothing else needs to be done.
As for your code itself the first thing is you are missing a return statement.
If each line in the file is a json object, then use this:
from __future__ import with_statement
import json
def my_function(parameter1):
tweets = []
with open(parameter1) as inf:
for line in inf:
try:
tweets.append(json.loads(line))
except:
pass
return tweets
If the enter file is a json object, then you can do this:
def my_function(parameter1):
tweets = None
with open(parameter1) as inf:
tweets = json.load(inf)
return tweets

How to import Python code from a source file?

I was hesitant about asking this because I've seen similar questions with overly complicated answers. The question I actually want answered to was closed on account of being "vague" and "not a real question", so I will be as specific as possible.
I have an object called "line" in a file called "line.py". I want to create another object that inherits from "line" called "line_segment" and put it in a file called "line_segment.py" in the same directory.
path_finding_lib/
line.py
line_segment.py
a_star.py
The problem is I can't seem to find a way to use the code in "line.py" from inside "line_segment.py" without appending strings to the system PATH variable and stuff like that.
Isn't there a way to import code from a file path or something like that? If not, why not?
While you could append to the python path for a particular file using:
import sys
sys.path.append('pathtoModule')
If they are in the same folder, (not a module, as you lack an init.py), you can simply import from the line module by doing:
from line import Line
class LineSegment(line):
Naming convention supplies underscores and lowercases for modules, Capitalized for Classes:
http://www.python.org/dev/peps/pep-0008/
It is nonstandard and will likely lead to trouble if you dynamically append to the python path in your project, as it will cause errors in object comparison.
If you have an object declared as follows:
path_finding_lib/
line.py
line_segment.py
a_star.py
somemodule/
afile.py
If your line module imports and instantiates an object from afile using the path somemodule.afile.SomeObject
It is not the same class as instantiating an object from within afile:
afile.SomeObject.
If afile returns an instance of afile.SomeObject and the object is compared for equality to an instance of somemodule.afile.SomeObject, they will be found to be not equivalent.
i.e.
somemodule.afile.SomeObject == afile.SomeObject
==> False
The easiest way to use python code in other files is to use import statement.
Say when you do
import xyz
Python will attempt to find the file xyz.py. It looks into
The site-packages folder (which is the folder in your python installation directory which contains pre-installed modules like say django etc)
Locations mentioned in PYTHONPATH environment variable (or sys.path in python)
Your current directory
In your case, your program should have the following line
from line import line
Where first line is your line.py file and second line is your class
Wherever you want to use line object for inheritance just use
class newline(line):
The catch is how you run the program. If you run it from within path_finding_lib (i.e. when your working directory is path_finding_lib and you do
python line_segment.py
, it should work (You can optionally also make a blank file init.py in the same folder).
If you run it from say your home directory
~$ python /path_to/path_finding_lib line_segment.py
It will NOT work. This is because python will search site-packages, PYTHONPATH and your current directory and not find line.py. To be able to run it from everywhere, before you run it add location of line.py to the PYTHONPATH
$export PYTHONPATH=/path_to/path_finding_lib
Then you should be able to run it
NOTE: I have assumed you have a linux ystem. For Windows unfortunately I do not know the procedure of modifying PYTHONPATH
Since they're in the same directory, create an empty file named __init__.py. This lets Python treat the directory you're working from as a package, and you'll be able to pull objects and methods from these other files.

Categories

Resources