Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2 & 3 compatible namespace modules (using pip)

Tags:

How is it possible build multiple python modules sharing the same namespace compatible for Python 2.7+ and 3.3+?

Let's call the namespace test. Now I want to have two seperate modules called test.foo and another one called test.bar. However, I'm currently developing test.helloworld which depends on both, test.foo and test.bar. Both are listed in the requirements.txt file.

The modules test.foo and test.bar are currently using the Python 2 solution for namespace packages:

import pkg_resources pkg_resources.declare_namespace(__name__) 

Running the suggested pip-command for development mode pip install -e . turns into: ImportError: No module named 'test.helloworld' while importing test.foo or test.bar is working.

The Python 3 solution for namespace packages are Implicit Namespace Packages where the namespace package has no __init__.py file. This is sadly not working for Python 2 versions.

How can I design a solution for both Python 2 and 3 (which allows me to use pip install -e .)? The --egg solution does not work for me since it is already deprecated.

like image 618
floqqi Avatar asked Aug 10 '15 09:08

floqqi


People also ask

What is the Python 2?

What is Python 2? Python 2 made code development process easier than earlier versions. It implemented technical details of Python Enhancement Proposal (PEP). Python 2.7 (last version in 2. x ) is no longer under development and in 2020 will be discontinued.

Is Python 2 or 3 better?

Python 3 is definitely more readable, easier to grasp, and popular than Python 2. Python 2 has definitely run out of steam and one should learn Python 2 if and only if some legacy code has been written in Python 2 or if a company needs the developer to migrate the Python 2 code into Python 3.

Will Python 2 ever end?

We have decided that January 1, 2020, was the day that we sunset Python 2. That means that we will not improve it anymore after that day, even if someone finds a security problem in it.

Can you still download Python 2?

The final version of Python 2.0 is available for download now.


1 Answers

I recently had a similar issue, where I had to install a package for Python 2 and 3. I ended up having to download the code from GitHub, then ran the setup.py by calling

sudo python setup.py install

and

sudo python3 setup.py install

This results in the package being installed for both Python 2 and 3, even though the code itself was meant for Python 2. This allows me to work with the package whether I use Python 2 or 3, without any namespace conflicts.

like image 167
awkbr549 Avatar answered Nov 21 '22 00:11

awkbr549