0

I am trying to install and import this python module in the same file. I am adding the directory where I want networkX to be installed in PYTHONUSERBASE, and I even prepend it to the PATH.

#install and import networkx
os.environ['PYTHONUSERBASE'] = "some_other_directory/python_packages"
executeCommand('python3 -m pip install --user networkx[default]')

sys.path.insert(0, os.environ['PYTHONUSERBASE'])
importlib.invalidate_caches()
import networkx

However, I get this error:

Traceback (most recent call last):
  File "file_that_runs_the_code.py", line 35, in <module>
    import networkx
ModuleNotFoundError: No module named 'networkx'

Any help would be appreciated.

pscarpati
  • 48
  • 1
  • 9

1 Answers1

0

The answer posted by @Tom McLean did solve the issue, but I would like to point out the following:

The answer provided might throw errors for pip 10 or higher. If your version of pip is higher than 10, I recommend replacing

pip.main(['install', package])

with

os.system('pip install ' + package)

For more information on the errors that might be thrown if you are using pip10+, check out this link: https://github.com/pypa/pip/issues/5599

pscarpati
  • 48
  • 1
  • 9