What is the pythonic way to share common files in multiple projects?

Lets say I have projects x and y in brother directories: projects/x and projects/y.
There are some utility funcs common to both projects in myutils.py and some db stuff in mydbstuff.py, etc.
Those are minor common goodies, so I don't want to create a single package for them.

Questions arise about the whereabouts of such files, possible changes to PYTHONPATH, proper way to import, etc.

What is the 'pythonic way' to use such files?

Asked by: David211 | Posted: 06-12-2021

Answer 1

The pythonic way is to create a single extra package for them.

Why don't you want to create a package? You can distribute this package with both projects, and the effect would be the same.

You'll never do it right for all instalation scenarios and platforms if you do it by mangling with PYTHONPATH and custom imports.

Just create another package and be done in no time.

Answered by: Paul655 | Posted: 07-01-2022

Answer 2

You can add path to shared files to sys.path either directly by sys.path.append(pathToShared) or by defining .pth files and add them to with site.addsitedir. Path files (.pth) are simple text files with a path in each line.

Answered by: Anna360 | Posted: 07-01-2022

Answer 3

You can also create a .pth file, which will store the directory(ies) that you want added to your PYTHONPATH. .pth files are copied to the Python/lib/site-packages directory, and any directory in that file will be added to your PYTHONPATH at runtime.

StackOVerflow question (see accepted solution)

Answered by: Lydia485 | Posted: 07-01-2022

Answer 4

I agree with 'create a package'.

If you cannot do that, how about using symbolic links/junctions (ln -s on Linux, linkd on Windows)?

Answered by: Wilson639 | Posted: 07-01-2022

Answer 5

I'd advise using setuptools for this. It allows you to set dependencies so you can make sure all of these packages/individual modules are on the sys.path before installing a package. If you want to install something that's just a single source file, it has support for automagically generating a simple setup.py for it. This may be useful if you decide not to go the package route.

If you plan on deploying this on multiple computers, I will usually set up a webserver with all the dependencies I plan on using so it can install them for you automatically.

I've also heard good things about paver, but haven't used it myself.

Answered by: Aida919 | Posted: 07-01-2022

Similar questions

python - More pythonic way to pick multiple element types from tagged data

I have some tagged data that I am processing using lxml. When I open a file I do not know before the file is opened if I have one or more of three types of elements (I could have one, two or three different elements and multiple instances of any type I have) I need some information about these elements that is contained in child tags of the element <element_type_1> <name>joe smi...

python - Pythonic way to import data from multiple files into an array

I'm relatively new to Python and wondering how best to import data from multiple files into a single array. I have quite a few text files containing 50 rows of two columns of data (column delimited) such as: Length=10.txt: 1, 10 2, 30 3, 50 #etc END OF FILE - Length=20.txt 1, 50.7 2, 90.9 3, 10.3 #etc END OF FILE Let's say I have 10 text fi...

python - What's the most pythonic way to iterate over all the lines of multiple files?

I want to treat many files as if they were all one file. What's the proper pythonic way to take [filenames] => [file objects] => [lines] with generators/not reading an entire file into memory? We all know the proper way to open a file: with open("auth.log", "rb") as f: print sum(f.readlines()) And we know the correct way to link several iterators/generators into one long one:

python - What's the pythonic method of doing multiple ors?

This question already has answers here:

python - Pythonic way to process multiple for loops with different filters against the same list?

Here's a bit of a program I'm writing that will create a csv categorizing a directory of files: matches = [] for root, dirnames, filenames in os.walk(directory): for filename in fnmatch.filter(filenames, '*[A-Z]*'): matches.append([os.path.join(root, filename), "No Capital Letters!"]) test = re.compile(".*\.(py|php)", re.IGNORECASE) for filename in filter(test.search, filenames): ...

python - Pythonic way to append multiple values to a list

This question already has answers here:

python - Is there a Pythonic way to delete common indexes in multiple lists?

Closed. This question needs to be more focused. It ...

python - Pythonic way of writing multiple regex tests

How to write in a Pythonic way when there are multiple regex patterns to test with and extract matched groups if a test succeeds? That is to say, what is the Pythonic equivalent of the following code snippet? if re.match(pattern1, string): m = re.match(pattern1, string) grps = m.groups() ...[process matched groups for pattern1]... elif re.match(pattern2, string): m = re.match(pattern...

python - Most pythonic way to plot multiple signals

I would like to plot one ore more signals into one plot. For each signal, a individual color, linewidth and linestyle may be specified. If multiple signals have to be plotted, a legend should be provided as well. So far, I use the following code which allows me to plot up to three signals. import matplotlib fig = matplotlib.figure.Figure(figsize=(8,6)) subplot = fig.add_axes([0.1, 0.2, 0.8, ...

python - Pythonic way to sort the records with multiple columns

Let's say we have a lot of records in a file in the following fashion. : Some SSL Vulnerability : Medium : Patched : Some RCE Vulnerability : High : Not Patched These records have the 3rd column with values Critical, High, Medium, Low. What's the best possible pythonic way to sort these records in this manner :...

python - Is it pythonic for a function to return multiple values?

In python, you can have a function return multiple values. Here's a contrived example: def divide(x, y): quotient = x/y remainder = x % y return quotient, remainder (q, r) = divide(22, 7) This seems very useful, but it looks like it can also be abused ("Well..function X already computes what we need as an intermediate value. Let's have X return that value also"). W...

python - A pythonic way to insert a space before capital letters

I've got a file whose format I'm altering via a python script. I have several camel cased strings in this file where I just want to insert a single space before the capital letter - so "WordWordWord" becomes "Word Word Word". My limited regex experience just stalled out on me - can someone think of a decent regex to do this, or (better yet) is there a more pythonic way to do this that I'm missing?

python - Is there a more Pythonic way to merge two HTML header rows with colspans?

I am using BeautifulSoup in Python to parse some HTML. One of the problems I am dealing with is that I have situations where the colspans are different across header rows. (Header rows are the rows that need to be combined to get the column headings in my jargon) That is one column may span a number of columns above or below it and the words need to be appended or prepended based on the spanning. Below is a routine to d...

python - Pythonic ways to use 'else' in a for loop

This question already has answers here:

python - pythonic way to compare compound classes?

I have a class that acts as an item in a tree: class CItem( list ): pass I have two trees, each with CItem as root, each tree item has some dict members (like item._test = 1). Now i need to compare this trees. I can suggest to overload a comparison operator for CItem: class CItem( list ): def __eq__( self, other ): # first compare items as lists if not list.__eq...

python - Pythonic URL Parsing

There are a number of questions about how to parse a URL in Python, this question is about the best or most Pythonic way to do it. In my parsing I need 4 parts: the network location, the first part of the URL, the path and the filename and querystring parts. http://www.somesite.com/base/first/secon...

list - Pythonic way to get some rows of a matrix

I was thinking about a code that I wrote a few years ago in Python, at some point it had to get just some elements, by index, of a list of lists. I remember I did something like this: def getRows(m, row_indices): tmp = [] for i in row_indices: tmp.append(m[i]) return tmp Now that I've learnt a little bit more since then, I'd use a list comprehension like this:

python - What is the Pythonic way to write this loop?

for jr in json_reports: jr['time_created'] = str(jr['time_created'])

python - How do you make this code more pythonic?

Could you guys please tell me how I can make the following code more pythonic? The code is correct. Full disclosure - it's problem 1b in Handout #4 of this machine learning course. I'm supposed to use newton's algorithm on the two data sets for fitting a logistic hypothesis. But they use matlab & I'm using scipy ...

python - Pythonic Swap of 2 lists elements

I found that I have to perform a swap in python and I write something like this: arr[first], arr[second] = arr[second], arr[first] I suppose this is not so pythonic. Does somebody know how to do a swap in python more elegant? EDIT: I think another example will show my doubts: self.memberlist[someindexA], self.memberlist[someindexB] = self.memberlist[som...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python