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?
if you are using linux ubuntu, do these followings in order.
sudo apt-get update
sudo apt-get install python-networkx
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
type networkx in search text box, and then select it from package
list and click on install
after finish it , click ok and close windows
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
Related
I am just learning to code using the wordcloud and stylecloud libraries in python 3.9.0. I wrote the basic code for the stylecloud library but it gives an error that says:
ImportError: cannot import name 'makeMappingArray' from 'matplotlib.colors'
This is the code:
import wordcloud
import matplotlib
def cloud_design():
stylecloud.gen_stylecloud(file_path=r"C:\Users\Edwin Anil\OneDrive - AL RAYAN HOLDING CO\ALL PYTHON CODES\word cloud text file.txt",icon_name="<i class='fas fa-guitar'></i>")
cloud_design()
The version of wordcloud that is being used is wordcloud-1.8.1.
The version of matplotlib that is being used is matplotlib-3.4.1.
this is the entire error message:
File "C:\Users\Edwin Anil\OneDrive - AL RAYAN HOLDING CO\ALL PYTHON CODES\word cloud.py", line 1, in <module>
import stylecloud
File "C:\Users\Edwin Anil\AppData\Local\Programs\Python\Python39\lib\site-packages\stylecloud\__init__.py", line 1, in <module>
from .stylecloud import gen_stylecloud
File "C:\Users\Edwin Anil\AppData\Local\Programs\Python\Python39\lib\site-packages\stylecloud\stylecloud.py", line 6, in <module>
from matplotlib.colors import makeMappingArray, to_rgb
ImportError: cannot import name 'makeMappingArray' from 'matplotlib.colors' (C:\Users\Edwin Anil\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\colors.py)```
I did try various other syntax forms for import and tried uninstalling and reinstalling matplotlib too but I don't seem to figure it out. Some help is appreciated.
when you copy the html,you just need to put the class name. example "fas fa-guitar":
import wordcloud
import matplotlib
import stylecloud
def cloud_design():
stylecloud.gen_stylecloud(file_path="my_random.txt",icon_name="fas fa-guitar")
cloud_design()
output:
guitar
Alright so according to #simpleApp's solution. I tried downgrading to an older version of matplotlib and it worked.
The cmd command was pip install matplotlib==3.2.2
Here is the result:
I have this structure
02.SensorTag/
sensortag_example.py
bluepy/bluepy/sensortag.py
bluepy/bluepy/btle.py
So the sensortag_example.py is importing the sensortag.py
import bluepy
from bluepy.bluepy import sensortag
When I ran the code it complains about the import from the sensortag.
Traceback (most recent call last):
File "sensortag_example.py", line 2, in <module>
from bluepy.bluepy import sensortag
File "/home/pi/Development/02.SensorTag/bluepy/bluepy/__init__.py", line 3, in <module>
from . import sensortag
File "/home/pi/Development/02.SensorTag/bluepy/bluepy/sensortag.py", line 1, in <module>
from bluepy.btle import UUID, Peripheral, DefaultDelegate, AssignedNumbers
ImportError: No module named 'bluepy.btle'
I've tried to add a new path but it didin't work. If I move the program to the first folder bluepy and change the import to "from bluepy import sensortag" it works, but I'll need to import other libs so I don't want to let it in bluepy folder.
I am trying to run this code:
https://gist.github.com/atotto/ae603b962115eef703c0011d8e652ea3
Thanks and best regards,
Edu
Because sensortag.py is in the same directory as btle.py, add a . in front of the import
from .btle import UUID, Peripheral, DefaultDelegate, AssignedNumbers
This is known as a relative import: https://docs.python.org/2.5/whatsnew/pep-328.html
As both btle.py and sensortag.py are in the same directory so by looking at your error I am assuming that you tried to import it from previous directory. So place from .btle import UUID in sensortag.py should solve the issue.
You should create two init.py file.
02.SensorTag/
sensortag_example.py
bluepy/__init__.py
bluepy/bluepy/__init__.py
bluepy/bluepy/sensortag.py
bluepy/bluepy/btle.py
Importing UTILS classes into Inventory - can it be done?
I have created a custom LDAP data importer as part of creating my inventory class. The LDAP schema we have wasn't similar enough to the LDAP plugin provided in samples.
My class is called ldapDataModule; the class is in:
/home/agt/ansible/agt_module_utils/ldapDataModule.py
My "$HOME/.ansible.cfg" file has the following:
module_utils = /home/agt/ansible/agt_module_utils
When running my Ansible inventory module, I get the following output:
ansible ecomtest37 -m ping
ERROR! Attempted to execute "/sites/utils/local/ansible/hosts" as
inventory script: Inventory script (/sites/utils/local/ansible/hosts) had
an execution error: Traceback (most recent call last):
File "/sites/utils/local/ansible/hosts", line 22, in
from ansible.module_utils import ldapDataModule
ImportError: No module named module.utils
The include statement inside hosts appears like this:
import copy
import ldap
import re
import sys
import operator
import os
import argparse
import datetime
import os.path
try:
import json
except:
import simplejson as json
from ansible.module_utils import ldapDataModule
class agtInventory(object):
RECOMENDATIONS?
I was able to do the following as a "work around". I'd still like to hear from Ansible guru's on proper use of "module_utils" variable from ansible.cfg
sys.path.insert(0, '/home/agt/ansible/agt_module_utils')
from ldapDataModule import ldapDataModule
In Ubuntu 14.04, I have installed Graphlab based on https://dato.com/download/install-graphlab-create-command-line.html and it seems to be working fine.
However, I receive this error when trying to use a recommender module:
import graphlab
from graphlab.recommender import ranking_factorization_recommender
In the first line, graphlab is imported without any error. However, the second line causes this error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-34df81ffb957> in <module>()
----> 1 from graphlab.recommender import ranking_factorization_recommender
ImportError: No module named recommender
How can the problem be solved? Thanks
It's just a namespace issue. recommender actually lives in the `toolkits module, so this should work:
import graphlab
from graphlab.toolkits.recommender import ranking_factorization_recommender
Graphlab has already imported everything for you in their __init__.py file.
Just do:
from graphlab import ranking_factorization_recommender
from graphlab import <any_other_recommender>
Here is a snippet of graphlab.__init__.py file:
from graphlab.util import get_runtime_config
from graphlab.util import set_runtime_config
import graphlab.connect as _mt
import graphlab.connect.aws as aws
from . import visualization
import os as _os
import sys as _sys
if _sys.platform != 'win32' or \
(_os.path.exists(_os.path.join(_os.path.dirname(__file__), 'cython', 'libstdc++-6.dll')) and \
_os.path.exists(_os.path.join(_os.path.dirname(__file__), 'cython', 'libgcc_s_seh-1.dll'))):
from graphlab.data_structures.sgraph import Vertex, Edge
from graphlab.data_structures.sgraph import SGraph
from graphlab.data_structures.sarray import SArray
from graphlab.data_structures.sframe import SFrame
from graphlab.data_structures.sketch import Sketch
from graphlab.data_structures.image import Image
from graphlab.data_structures.sgraph import load_sgraph, load_graph
from graphlab.toolkits._model import Model, CustomModel
import graphlab.aggregate
import graphlab.toolkits
import graphlab.toolkits.clustering as clustering
import graphlab.toolkits.distances as distances
...
I have some troubles with importation of self-made modules, I just can't see what I am doing wrong.
I have a package named basics, which has all my base classes
I have a second package named components, and every module in componentsuses the modules from basics.
I have a script file, located in another folder, which calls upon the basics and components modules.
I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "H:/scripts/CIF_utilities/scripts/hello world.py", line 11, in <module>
TW=TextWriter(r'H:/scripts/CIF_utilities/components')
File "H:\scripts\CIF_utilities\components\textwriter.py", line 23, in __init__
layout=Layout(File=os.path.join(path,'alphabet.CIF'))
NameError: global name 'Layout' is not defined
There is my script: hello world.py
#hello world.py
import basics
from components.textwriter import *
TW=TextWriter(r'H:/scripts/CIF_utilities/components')
cell=TW.writeText('Hello World',30e3)
cell.draw()
layout=Layout()
layout.addCell(cell)
layout.workCell=cell
layout.exportCIF('hello world',os.getcwd())
textwriter.py is the one giving the error. In init, I load some data from a preformatted file using the Layout class (which will make the import)
in textwriter.py
#texwriter.py
import basics
import os, os.path, sys
import re
from numpy import *
from scipy import *
class TextWriter:
def __init__(self,pathToCIF=None):
if pathToCIF==None:
path=os.path.split(textwriter.__file__)[0]
else:
path=pathToCIF
###line that crashes is here
layout=Layout(File=os.path.join(path,'alphabet.CIF'))
self.alphabet=layout.workCell
There is the layout.py class:
#layout.py
import basics
from numpy import *
from scipy import *
import Tkinter
import tkFileDialog
import os, os.path
import re
import datetime
class Layout:
countCell=0
#classmethod
def getNewNumber(self):
Layout.countCell+=1
return Layout.countCell
def __init__(self,File=None):
self.cellList=[]
self.layerList=[]
self.nameFile=""
self.comments=""
self.workCell=None
if File!=None:
self.importCIF(File)
the init.py of the basics package contains all the necessary importations:
#__init__.py in basics folder
from baseElt import *
from cell import *
from layout import *
from transformation import *
the init.py from components is empty
I am currently using the anaconda 64bits distribution (python 2.7 if I recall well)
Thanks for your much needed help!
Since Layout is imported in basics/__init__.py, it only exists in the basics namespace, not in helloworld.py. Either access it with
layout = basics.Layout()
or explicitly import Layout into helloworld.py with
from basics import Layout
In textwriter.py, you want to switch
layout=Layout(File=os.path.join(path,'alphabet.CIF'))
for
layout=basics.Layout(File=os.path.join(path,'alphabet.CIF'))
You may have similar problems along your code. it is good to note that it is not pythonic to use
from package import *
It is recommended to instead use
from package_or_module import specific_item
import package_or_module
Remember, if you import with regular import you have to use the module/package name to access the object you want (i.e. basics.Layout).