0

I need to submit an assignment for my uni which involves a graph. Since external modules are allowed I used networkx to get the job done and I need to submit the algorithm to an online checker to see if my solution works with different inputs. The problem is that I need to submit a zip with my code and the libraries in a /lib subfolder. What I did was execute networkx's setup.py, I copied the lib folder from the build one and I put it in my project zip but the website tells me this: Error

So my final tree is

-file.py

-lib/networkx/....

Does anyone know how to solve this type of issue?

  • 2
    libraries are not always pure and depend on another library. thus you need to include all dependencies in your pack. please check similar topics if there is an answer to do this all at once until someone gives an answer to yours – Yılmaz Durmaz Oct 28 '20 at 15:46

1 Answers1

0

tl/dr python -m pip install -I --only-binary=":all:" --target lib networkx, then add import sys; sys.path.append('path/to/lib') to your script.


The problem is that in order for networkx to work, you'd need to include all of its dependencies and all of their dependencies in lib. Unfortunately, running setup.py won't do this for you, and not all of these dependencies will work on all platforms. For example, one of networkx's dependencies is numpy, which is implemented in C rather than python and has to be compiled separately for different target architectures.

All of this complexity illustrates why Python programs aren't usually distributed this way. If your program checker has access to the internet, the easiest solution would be to add a couple of lines to your program to install networkx via pip.

If you can't install packages over the internet, you can pre-build networkx and its dependencies in the lib directory with the following command.

python -m pip install -I --only-binary=":all:" --target lib networkx

The -I option is to ignore packages already installed on your machine. This should make sure all the necessary dependencies end up in lib. You might need to use the --platform and --python-version flags to make sure you get the right versions of networkx and its dependencies for the online checker. If you don't know the platform or python version the online checker uses, add a couple of lines to your program to print the output of sys.platform and sys.version before you try to import networkx.

You'll also probably need to add lib to the search path to make sure the python interpreter can find the modules you put in there. You can use sys.path.append('path/to/lib') from the builtin sys module for this.

Emerson Harkin
  • 889
  • 5
  • 13
  • I just tried but it won't let me use pip on the website, I asked the professor and he only told me that other students managed to do it so there must be a way but I just can't find it – DisguisedToucan Oct 28 '20 at 16:00
  • That's a shame, @DisguisedToucan. That would have been the easiest way to go. I edited my answer to include instructions for compiling networkx to `lib` using pip on your local machine, but you'll have to be careful to supply the right python version and platform to match what the online checker uses. – Emerson Harkin Oct 28 '20 at 16:46