Rachelle Mariano
Rachelle Mariano

Reputation: 1

Python: Import networkx as nx: Global name not defined

I wrote a module (processing_0) in which I import all packages and modules required for my project.

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import collections
import la
import csv
import fishery
import re
from collections import OrderedDict

import processing_1
import processing_2
import processing_3

from processing_1 import readingraph, readinpathgraph, preparefisher, inEEG
from processing_2 import pathwayprofile
from processing_3 import checkkin
from fishery import fisher

The modules that I wrote (processing_1/2/3) all require access to networkx (nx).

As part of the master module, I have a the startup function:

def startup():

  EEG = readingraph("/.../file1")
  EET = readingraph("/.../file2")
  EEL = readingraph("/.../file3")

  return EEG, EET, EEL

However, after importing processing_0 and trying to run startup() that uses readingraph from processing_1, I keep getting the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "processing_0.py", line 31, in startup
    EEG = readingraph("/.../file1")
  File "processing_1.py", line 4, in process
    graph = nx.read_adjlist(filename)
NameError: global name 'nx' is not defined

Is there any way to globally import networkx as nx and make it accessible to all imported modules?

Upvotes: 0

Views: 7507

Answers (2)

Shohreh pour siami
Shohreh pour siami

Reputation: 11

if you are using linux ubuntu, do these followings in order.

  1. sudo apt-get update
  2. sudo apt-get install python-networkx
  3. go to pycharm env and > file>setting> interpreter and structure to configure your python env and add packages, there is all packages that are in env, then click on + to add newpackage
  4. type networkx in search text box, and then select it from package list and click on install
  5. after finish it , click ok and close windows

Upvotes: 1

bt3gl
bt3gl

Reputation: 31

In every file that you use networkx you need to import it. So just repeat the line import networkx as nx inside the file processing_1.py

Upvotes: 0

Related Questions