How to import an Excel file to Python? [New to Python] - python

I'm currently using the following code (as learned in the DataCamp courses I followed):
import pandas as pd
pd.read_excel('C:\Users\jeenb\OneDrive\Bureaublad\Master Finance\Master Thesis\Input Python\Training Set.xlsx')
I'm getting the following error message:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
I've tried the following as was suggested in this forum before:
pd.read_excel('C:\\Users\\jeenb\\OneDrive\\Bureaublad\\Master Finance\\Master Thesis\\Input Python\\Training Set.xlsx')
pd.read_excel(r'C:\Users\jeenb\OneDrive\Bureaublad\Master Finance\Master Thesis\Input Python\Training Set.xlsx')
pd.read_excel(r"C:\Users\jeenb\OneDrive\Bureaublad\Master Finance\Master Thesis\Input Python\Training Set.xlsx")
All of these give the following error message:
Traceback (most recent call last):
File "C:\Users\jeenb\AppData\Roaming\Python\Python310\site-packages\pandas\compat\_optional.py", line 126, in import_optional_dependency
module = importlib.import_module(name)
File "C:\Program Files\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'openpyxl'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
pd.read_excel(r"C:\Users\jeenb\OneDrive\Bureaublad\Master Finance\Master Thesis\Input Python\Training Set.xlsx")
File "C:\Users\jeenb\AppData\Roaming\Python\Python310\site-packages\pandas\util\_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "C:\Users\jeenb\AppData\Roaming\Python\Python310\site-packages\pandas\io\excel\_base.py", line 457, in read_excel
io = ExcelFile(io, storage_options=storage_options, engine=engine)
File "C:\Users\jeenb\AppData\Roaming\Python\Python310\site-packages\pandas\io\excel\_base.py", line 1419, in __init__
self._reader = self._engines[engine](self._io, storage_options=storage_options)
File "C:\Users\jeenb\AppData\Roaming\Python\Python310\site-packages\pandas\io\excel\_openpyxl.py", line 524, in __init__
import_optional_dependency("openpyxl")
File "C:\Users\jeenb\AppData\Roaming\Python\Python310\site-packages\pandas\compat\_optional.py", line 129, in import_optional_dependency
raise ImportError(msg)
ImportError: Missing optional dependency 'openpyxl'. Use pip or conda to install openpyxl.
I'm typing this code in the IDLE Shell 3.10.2. Previously I got an error message while importing the pandas however since I don't get this anymore I feel like I fixed this.

Seems like you need to open your command prompt, activate the environment you are using and type: pip install openpyxl

I found the answer. Thought I would post it if anyone in the future would be struggeling with this and found this post. My slashes were facing the wrong way, just changing \ to / fixed the problem.

In my case of using pycharm, I needed to go to File -> settings -> Project: projectName -> Python interpreter. Then click on + button will show you a pop up window to install packages. Then you can search for openpyxl and get the latest package. Then click Install package to install it.

Related

TypeError: translation() got an unexpected keyword argument 'codeset'

I'm following a Python tutorial on youtube and need to create a django website, however I am unable to start, because when I enter "python manage.py runserver" I get the "TypeError: translation() got an unexpected keyword argument 'codeset'" message. I've run back the video like 20 times to see if I've missed anything, but no, because it's just the beginning of the django website tutorial. I've also tried typing python3 instead of python and some other options I saw on Stack Overflow, but none are really exactly relevant to the error message I'm getting. Perhaps someone knows how to fix this?
I tried to start a development server by typing in "python manage.py runserver" which was supposed to start a django webserver at 127.0.0.1:8000 or something, but instead I got the error message specified in the title
code:
PS C:\Users\kaspa\PycharmProjects\PyShop> python manage.py runserver
Exception ignored in thread started by: <function check_errors.<locals>.wrapper at 0x00000145784C1F80>
Traceback (most recent call last):
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\auth\models.py", line 94, in <module>
class Group(models.Model):
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 139, in __new__
new_class.add_to_class(obj_name, obj)
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 304, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\fields\related.py", line 1583, in contribute_to_class
self.remote_field.through = create_many_to_many_intermediary_model(self, cls)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\fields\related.py", line 1051, in create_many_to_many_intermediary_model
'verbose_name': _('%(from)s-%(to)s relationship') % {'from': from_, 'to': to},
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\functional.py", line 149, in __mod__
return str(self) % rhs
^^^^^^^^^
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\functional.py", line 113, in __text_cast
return func(*self.__args, **self.__kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\translation\__init__.py", line 75, in gettext
return _trans.gettext(message)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\translation\trans_real.py", line 286, in gettext
_default = _default or translation(settings.LANGUAGE_CODE)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\translation\trans_real.py", line 199, in translation
_translations[language] = DjangoTranslation(language)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\translation\trans_real.py", line 90, in __init__
self._init_translation_catalog()
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\translation\trans_real.py", line 131, in _init_translation_catalog
translation = self._new_gnu_trans(localedir)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\translation\trans_real.py", line 119, in _new_gnu_trans
return gettext_module.translation(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: translation() got an unexpected keyword argument 'codeset
'
I've also been following Mosh's Python course, ran into the same problem, came here for answers and then did some more research on my own.
I'm a total beginner, I might me wrong, but in the tutorial, Mosh makes us install django 2.1 instead of the current version of django. The error "translation() got an unexpected keyword argument 'codeset'" basically means that python thinks there shouldn't be an argument called 'codeset' after the gettext_module.translation( that is in the code created with/(used in?) django 2.1.
Turns out, in the python gettext documentation, "Changed in version 3.11: codeset parameter is removed.". So django 2.1 has some code that isn't reconized by python 3.11, which pops an error.
Solution:
1- Deleted my python "PyShop" project.
2- Closed PyCharm.
3- Created a new "PyShop" project.
4- In the terminal, typed "pip uninstall django".
5- Then installed the current version of django by typing "pip install django" in the terminal (not pip install django==2.1 as shown in the tutorial).
6- Created the "pyshop" folder by typing "django-admin startproject pyshop ." in the terminal.
7- Typed "python manage.py runserver" in the terminal.
And voila! No more error, because the current version of django doesn't create stuff with code that the current version of python doesn't recognize in it.
New to python and stack overflow, sorry for some things I maybe wrote wrong or something :)
The error refers to the file trans_real.py.
According to error message:
TypeError: translation() got an unexpected keyword argument 'codeset'
'codeset' is not a valid keyword. So comment it (place # at the beginning of the line).
It worked for me.
You can uninstall the django 2.1 using pip uninstall django==2.1
and reinstall the django that is compatible with your current interpreter (Python3) using pip install django.

Spyder not launching from one of the Anaconda environments

I'm new to this. I have begun learning OpenCV with Python and following the course directions I've successfully created an Anaconda environment from which I can easily import CV2 and carry out my course. So far, I have experienced no problems in working with this environment.
Now, I would like to begin using Spyder which launches well from the default/base environment but does not launch from the "opencv-course" environment which I created. By contrast, Jupyter Labs and Notebooks launch well on either environment.
I've attempted to uninstall/reinstall Spyder using Administrator privileges when launching Anaconda, but to no avail.
Could anyone forward a suggestion?
Many thanks!
MD
The following error message is displayed:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\opencv-course\Scripts\spyder-script.py", line 10, in
sys.exit(main())
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\app\start.py", line 205, in main
mainwindow.main()
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\app\mainwindow.py", line 3734, in main
mainwindow = run_spyder(app, options, args)
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\app\mainwindow.py", line 3590, in run_spyder
main.setup()
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\app\mainwindow.py", line 977, in setup
'spyder.plugins.{}'.format(plugin_name))
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\plugins\pylint\__init__.py", line 14, in
from .plugin import Pylint as PLUGIN_CLASS
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\plugins\pylint\plugin.py", line 29, in
from spyder.plugins.pylint.confpage import PylintConfigPage
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\plugins\pylint\confpage.py", line 16, in
from spyder.plugins.pylint.widgets.pylintgui import PylintWidget
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\plugins\pylint\widgets\pylintgui.py", line 37, in
from spyder.plugins.pylint.utils import get_pylintrc_path
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\plugins\pylint\utils.py", line 16, in
import pylint.config
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\pylint\config.py", line 54, in
import toml
ModuleNotFoundError: No module named 'toml'
After checking out this answer here:
Spyder failed to launch in Anaconda after update (4.1.2)
I've solved the problem by downgrading the version of Pylint from 2.5 to 2.4.4. It now works fine.
Many thanks!
MD

How to find out the reason and solution to this problem of import python modules in Vim

I am trying to set up python in Vim, but I failed to get it to work. It always throws an exception named UnicodeDecodeError.
I have installed gvim on windows 10. And also installed python 3 with the corresponding version.
Vim can find the python37.dll and the command
:echo has('python3')
returns 1 as expected.
My vim with python works only when no modules except for the builtin ones are imported.
For example:
:py3 print('a')
works pretty well.
:py3 import vim
or
:py3 import sys
are also working.
However, if I write a simple python script vim_test.py like this
print('This is for vim test')
then try to import it in vim as
:py3 import vim_test
it will give an exception:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 963, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 906, in _find_spec
File "<frozen importlib._bootstrap_external>", line 1280, in find_spec
File "<frozen importlib._bootstrap_external>", line 1252, in _get_spec
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 9: invalid start byte
It cannot import any python module in Vim.
But it can run this script directly by
:py3file vim_test.py
if the script file vim_test.py is in the current directory.
What could be the reason for this problem?
And how can I solve it?
I expected to be able to use vim plugins written in python.
With this problem, I cannot achieve that.

Sublime Text 3 control panel undocumented error

Putting together a new sublime install and trying to install the Control Panel. Have been through all of the standard forums, but just in the position of getting a mystery traceback regardless of installation method.
Traceback (most recent call last):
File "/opt/sublime_text/sublime_plugin.py", line 74, in reload_plugin
m = importlib.import_module(modulename)
File "./importlib/__init__.py", line 90, in import_module
File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
File "/opt/sublime_text/sublime_plugin.py", line 678, in load_module
exec(compile(source, source_path, 'exec'), mod.__dict__)
File "Package Control in /home/slater/.config/sublime-text-3/Installed Packages/Package Control.sublime-package", line 14, in <module>
File "./posixpath.py", line 83, in join
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'
plugins loaded
Unable to open /home/slater/.config/sublime-text-3/Packages/Package Control/Package Control.sublime-settings
Unable to open /home/slater/.config/sublime-text-3/Packages/User/Package Control.sublime-settings
Unable to open /home/slater/.config/sublime-text-3/Packages/Default/Preferences.sublime-settings
Presumably, this traceback is why things aren't functional, but unfortunately it's completely impenetrable. It references code that doesn't exist that seems to be making a relative call to another file that doesn't exist. I tried opening up the Package Control zip file, but since the console isn't giving me a real traceback and there's no file called posixpath.py it's very unhelpful for debugging.
It seems like this is some issue with paths? The call to the sublime files in opt is also throwing me for a loop, but I'm not certain that's related the the problem I'm seeing.
UPDATE: Have tried adding sublime package files to $PATH and $PYTHONPATH. Have also experimented with permissions, all to no avail.

Ansible - "NameError: name 'urllib2' is not defined"

Getting this below error while trying to run ansible(version >2) with python 3.5.2
I have looked into the github issues terming it as resolved, but can't sort out what needs to be done. https://github.com/ansible/ansible/issues/16013
How to resolve this?
virtual#xxxxxxxxxx:~/ansible-spike> ansible all -m ping -vvv
Using /home/virtual/ansible-spike/ansible.cfg as config file
ERROR! Unexpected Exception: name 'urllib2' is not defined
the full traceback was:
Traceback (most recent call last):
File "/home/virtual/.pyenv/versions/3.5.2/bin/ansible", line 92, in <module>
exit_code = cli.run()
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/cli/adhoc.py", line 193, in run
result = self._tqm.run(play)
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/executor/task_queue_manager.py", line 202, in run
self.load_callbacks()
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/executor/task_queue_manager.py", line 171, in load_callbacks
for callback_plugin in callback_loader.all(class_only=True):
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/plugins/__init__.py", line 368, in all
self._module_cache[path] = self._load_module_source(name, path)
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/plugins/__init__.py", line 319, in _load_module_source
module = imp.load_source(name, path, module_file)
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/imp.py", line 172, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 693, in _load
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/plugins/callback/hipchat.py", line 32, in <module>
from ansible.module_utils.urls import open_url
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/module_utils/urls.py", line 330, in <module>
if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib2, 'HTTPSHandler'):
NameError: name 'urllib2' is not defined
Urllib2 is specific to Python v2.
Urllib2 documentation at http://docs.python.org/library/urllib2.html:
The urllib2 module has been split across several modules in Python 3.0
named urllib.request and urllib.error.
I don't think Ansible is compatible with Python 3 yet.
The ansible python API does not support Python 3. The PyPI page lists only 2.6 and 2.7 .
Ansible is currently not able to run with Python3. That is also stated in the linked Github Issue.
If you are using
import urllib2
Instead, you can call the library as
import urllib.request
Then use it like
urllib.request.urlopen('your url goes here')
This should get rid of the error you are getting

Categories

Resources