0

I am quite new to Python and Django. I have a problem with integrating a python package (openpyxl) to my django app. I'd like to use the methods of these files into my views.py file.

My problem is first that I don't know where's the best place to put the openpyxl folder containing all the files in my file hierarchy. My hierarchy looks like this: https://i.stack.imgur.com/Ph7II.jpg

Is it well placed? Should I put it outside the international folder? inside the carte_interactive folder?

And my biggest problem is inside the __init__.py of openpyxl. I get errors lines like this one:
from openpyxl.xml import LXML

Where there is no resolved reference to LXML, but is actually defined in the xml file of openpyxl. Is it my bad file placement that caused this? or is it Django?, or is it openpyxl's fault? Do anyone have an idea?

You can see openpyxl's source files here, where I downloaded them: https://bitbucket.org/openpyxl/openpyxl/src

If you need any more details, please ask!
Thanks in advance!

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • 1
    Try doing `pip install openpyxl` and then you can do `from openpyxl import LXML ` – jape May 05 '16 at 23:12
  • @jape that will allow him to use openpyxl locally, but it won't include the dist with his Django app -- so if he packages it up, it will depend on openpyxl but will not contain it (other users will need to pip it as well). correct me if I am wrong – tenwest May 05 '16 at 23:59
  • @tenwest That's a good point. If he wanted to take the route you mentioned, he could place `openpyxl` as an app alongside `carte_interactive`, `international`, etc. I suppose a third option would be to add `openpyxl` to `requirements.txt` and include that in the packaged contents. -- That may be an answer to your question as well. – jape May 06 '16 at 00:41
  • @tenwest he should be doing the same in production. If he uses a requirements.txt file, he can install the same packages in dev and prod (services like Heroku will use this file automatically). – Daniel Roseman May 06 '16 at 07:17
  • Thanks! Didn't think of requirements.txt ! And installing the package on my computer worked too! – Gabrielle Bastien May 09 '16 at 13:22

2 Answers2

0

I applaud your enthusiasm for wanting to learn Django while being new to Python. That said, the way you have things set up right now will make your life unnecessarily difficult to manage.

I would first recommend reading up on best practices for setting up a Django project. Just doing a quick google search for "Django project layout best practices" will give you a lot of resources, but they'll all essentially tell you to do what's in the SO answer above.

The second very basic thing is using pip to install and use other python packages. This is especially important for a django project, where you often have a lot of dependencies outside of Django. Pip is a program to install additionaly python packages. They get installed in your PYTHONPATH, which is just a list of filepaths on disk where python will look for additional packages. If you're on a *NIX system, this is usually in something like /usr/lib/python2.7/. Once you have something in your python path, you can from any piece of code, use other libraries you've installed via the python import system. Essentially, all this more or less does is look through each location in your PYTHONPATHs for the library you're trying to import.

Finally, in regards specifically to lxml, you will want to install it via apt or some other package installer. (e.g. on ubuntu, apt install python-lxml

In order to keep track of all your external python-dependencies, stuff them in a file named "requirements.txt" in the top level directory. This is a pretty standard thing to do for Django projects, so don't worry about shipping code with ALL dependencies inside the project.

Community
  • 1
  • 1
Thtu
  • 1,992
  • 15
  • 21
  • lxml is only a soft dependency for openpyxl which will detect if it is there. The problem the OP has is because he is not following the instructions on installing openpyxl correctly: use pip. – Charlie Clark May 06 '16 at 07:48
0

Thanks to all of you! I'm using Jetbrains Pycharm and when I wrote import openpyxl, it gave me the choice to install the package. I suppose it does it with pip, which would certainly have worked the same. And I put the package in requirements.txt, so that other users would only have to install this requirement!

It works now! And thanks for the link on the best practices. I'll read that!