XlsxWriter

I am a new user. of SWAN.
Yesterday I ran a Python script which imports XlsxWriter, which worked. Today I get an error message:
ModuleNotFoundError Traceback (most recent call last)
in
1 import numpy as np
2 import pandas as pd
----> 3 import XlsxWriter

I tried using:
pip install --user XlsxWriter
from the bash prompt, but get the message:
Requirement already satisfied: XlsxWriter in ./.local/lib/python3.7/site-packages (1.2.8)

Any suggestions, please? Thanks.

Mike

Hi @michaeljbarnes

try with

import xlsxwriter

without any capital letter and let me know.

Cheers
Omar.

Hi Omar
A different error message

ImportError Traceback (most recent call last)
in
1 import numpy as np
2 import pandas as pd
----> 3 import xlsxwriter
4 # pip install --user XlsxWriter
5

~/.local/lib/python3.7/site-packages/xlsxwriter/init.py in
1 version = ‘1.2.8’
2 VERSION = version
----> 3 from .workbook import Workbook

~/.local/lib/python3.7/site-packages/xlsxwriter/workbook.py in
17 from struct import unpack
18
—> 19 from .compatibility import int_types, num_types, str_types, force_unicode
20
21 # Package imports.

ImportError: cannot import name ‘int_types’ from ‘xlsxwriter.compatibility’ (/eos/user/b/barnesm/.local/lib/python3.7/site-packages/xlsxwriter/compatibility.py)

I can not to reproduce that error, please try to run the next code in a new notebook

import xlsxwriter
# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()

# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)

# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})

# Write some simple text.
worksheet.write('A1', 'Hello')

# Text with formatting.
worksheet.write('A2', 'World', bold)

# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)

# Insert an image.
worksheet.insert_image('B5', 'logo.png')

workbook.close()

Hi Omar,
Thanks for your suggestion. However, I get a similar error:

ImportError Traceback (most recent call last)
in
----> 1 import xlsxwriter
2 # Create an new Excel file and add a worksheet.
3 workbook = xlsxwriter.Workbook(‘demo.xlsx’)
4 worksheet = workbook.add_worksheet()
5

~/.local/lib/python3.7/site-packages/xlsxwriter/init.py in
1 version = ‘1.2.8’
2 VERSION = version
----> 3 from .workbook import Workbook

~/.local/lib/python3.7/site-packages/xlsxwriter/workbook.py in
17 from struct import unpack
18
—> 19 from .compatibility import int_types, num_types, str_types, force_unicode
20
21 # Package imports.

ImportError: cannot import name ‘int_types’ from ‘xlsxwriter.compatibility’ (/eos/user/b/barnesm/.local/lib/python3.7/site-packages/xlsxwriter/compatibility.py)

please try reinstalling the package with the next command

pip install --upgrade --force-reinstall --user XlsxWriter

maybe something was wrogn in the installation.

The reinstall has been done:
bash-4.2$ pip install --upgrade --force-reinstall --user XlsxWriter
Collecting XlsxWriter
Downloading https://files.pythonhosted.org/packages/00/1f/2092a81056d36c1b6651a645aa84c1f76bcee03103072d4fe1cb58501d69/XlsxWriter-1.2.8-py2.py3-none-any.whl (141kB)
|████████████████████████████████| 143kB 4.0MB/s
Installing collected packages: XlsxWriter
Found existing installation: XlsxWriter 1.2.8
Uninstalling XlsxWriter-1.2.8:
Successfully uninstalled XlsxWriter-1.2.8
Successfully installed XlsxWriter-1.2.8

However, I see the same problem. I also tried putting
import xlsxwriter to the first line of my file, but still the same issue.
ImportError Traceback (most recent call last)
in
----> 1 import xlsxwriter
2 import numpy as np
3 import pandas as pd
4 #import xlsxwriter
5 # pip install --user XlsxWriter

~/.local/lib/python3.7/site-packages/xlsxwriter/init.py in
1 version = ‘1.2.8’
2 VERSION = version
----> 3 from .workbook import Workbook

~/.local/lib/python3.7/site-packages/xlsxwriter/workbook.py in
17 from struct import unpack
18
—> 19 from .compatibility import int_types, num_types, str_types, force_unicode
20
21 # Package imports.

ImportError: cannot import name ‘int_types’ from ‘xlsxwriter.compatibility’ (/eos/user/b/barnesm/.local/lib/python3.7/site-packages/xlsxwriter/compatibility.py)

Would it help if I shared the project with you?
Mike

Please open the swan terminal and run

export PYTHONPATH=$CERNBOX_HOME/.local/lib/python3.7/site-packages:$PYTHONPATH

now try in the same terminal,
ipython
and
import xlsxwriter

Hi Omar,
That seems to work:
bash-4.2$ export PYTHONPATH=$CERNBOX_HOME/.local/lib/python3.7/site-packages:PYTHONPATH bash-4.2 ipython
Python 3.7.6 (default, Mar 13 2020, 15:33:01)
Type ‘copyright’, ‘credits’ or ‘license’ for more information
IPython 7.5.0 – An enhanced Interactive Python. Type ‘?’ for help.

In [1]: import xlsxwriter

In [2]:

Great!

I think exporting that variable in your .bashrc should work for notebooks also,
lets add that line at the end of that file in the swan terminal running the next command

echo "export PYTHONPATH=$CERNBOX_HOME/.local/lib/python3.7/site-packages:PYTHONPATH" >> ~/.bashrc

be careful, we dont want to break that file.

restart your swan session and try again.

Hi Omar,
I think I am a little confused. Should I open a new swan terminal and and run:
echo "export PYTHONPATH=$CERNBOX_HOME/.local/lib/python3.7/site-packages:PYTHONPATH" >> ~/.bashrc ?
I tried this, closed my browser completely, reopened my browser, started a swan session and tried running the original .ipynb script - but I still have the same problem. Did I misunderstand your instructions?
Mike

we did a mistake,

the right line to write in .bashrc is
“export PYTHONPATH=$CERNBOX_HOME/.local/lib/python3.7/site-packages:$PYTHONPATH”

lets take a look, is $PYTHONPATH at the end, in the answer you have it without the $.

then lets try again this solution.

open the swan terminal and with an editor lets edit this line in .bashrc, setting the right one
"export PYTHONPATH=$CERNBOX_HOME/.local/lib/python3.7/site-packages:$PYTHONPATH"

lets try again and let me know

Hi Omar,

I tried:
export PYTHONPATH=$CERNBOX_HOME/.local/lib/python3.7/site-packages:$PYTHONPATH
then:
$PYTHONPATH

It looks like $PYTHONPATH isn’t correct as it shows the path ‘twice’:
bash-4.2$ export PYTHONPATH=$CERNBOX_HOME/.local/lib/python3.7/site-packages:$PYTHONPATH

bash-4.2$ $PYTHONPATH

bash: /eos/user/b/barnesm//.local/lib/python3.7/site-packages:/eos/user/b/barnesm//.local/lib/python3.7/site-packages:PYTHONPATH: No such file or directory

Hi @michaeljbarnes,

if you have now the right line on ~/.bashrc and you restart your session it should work,
take a look carefully in the .bashrc maybe you have that line twice. if that is the case, remove one a check you leave the right one.

after that you can check it using echo $PYTHONPATH in the swan terminal.

Hi Omar,
I logged out of my SWAN sessio, then logged back in. I then typed echo PYTHONPATH which showed the following, i.e. without a dollar sign after the ':' and before PYTHONPATH bash-4.2 echo $PYTHONPATH
/eos/user/b/barnesm//.local/lib/python3.7/site-packages:PYTHONPATH
is this correct, please?

I then opened my notebook and tried running the script - but I still get the same error:
ImportError Traceback (most recent call last)
in
----> 1 import xlsxwriter
2 import numpy as np
3 import pandas as pd
4 #import xlsxwriter
5 # pip install --user XlsxWriter

~/.local/lib/python3.7/site-packages/xlsxwriter/init.py in
1 version = ‘1.2.8’
2 VERSION = version
----> 3 from .workbook import Workbook

~/.local/lib/python3.7/site-packages/xlsxwriter/workbook.py in
17 from struct import unpack
18
—> 19 from .compatibility import int_types, num_types, str_types, force_unicode
20
21 # Package imports.

ImportError: cannot import name ‘int_types’ from ‘xlsxwriter.compatibility’ (/eos/user/b/barnesm/.local/lib/python3.7/site-packages/xlsxwriter/compatibility.py)

Maybe I will try rebooting my PC…

Hi Omar,
I rebooted my PC. Opened a swan terminal and checked PYTHONPATH: bash-4.2 echo $PYTHONPATH
/eos/user/b/barnesm//.local/lib/python3.7/site-packages:PYTHONPATH

Then opened by Python notebook and ran the script. However, the error message is unchanged.
I tested other scripts, e.g. where I import different modules, and they work OK. So I added import xlsxwriter to one of these - and it didn’t complain. Hence, it only seems to be a problem in this Python script…
Mike

Hi @michaeljbarnes

great! but if you want we can to fix it completly.

you dont need to reboot your pc, you dont worry be patient, I am here to help you.
I can see that the line is not right,
lets do the next… in the swan terminal.

  1. lets do a backup of the original file
    cp ~/.bashrc ~/.bashrc.bk
  2. lets try this command to fix that line
    sed -i 's/:PYTHONPATH/:$PYTHONPATH/g' ~/.bashrc

That should fix it.

Cheers
Omar.

Hi Omar,
Thank you for your help. The Python script is working correctly now.
Have a good weekend.
Mike

great! @michaeljbarnes

have a good weekend too,

Omar.