Code runs in Spyder but errors out in Anaconda Prompt - python

I've written a script in Python to execute an external .sql file and export the results to Excel. I've written the script in Spyder(Python 3.6). When I run the complete file in Spyder, it runs and exports the data to Excel with no errors. I've tried to run the script in Anaconda Prompt, but I get an error relating to the Sybase ODBC Driver.
I've investigated this error, however I don't understand why it's not appearing when the code is run within Spyder, only when run in the Anaconda prompt. Is there a way to check if these environments are different?
I've investigated this error by searching the internet, however I don't understand why it's not appearing when the code is run within Spyder, only when run in the Anaconda prompt. Is there a way to check if these environments are different? Ideally, I'd be able to run my script on the command line, not only through an IDE.
The error that I get from the Anaconda Prompt is the following:
"pyodbc.Error: ('ZZZZZ', "[ZZZZZ] [Sybase][ODBC Driver][Adaptive Server Enterprise]The 'CREATE TABLE' command is not allowed within a multi-statement transaction in the 'tempdb' database.\n (2762) (SQLExecDirectW)")
"
I expect that running the script in Anaconda Prompt should give the exact same results (no error, successful export to Excel file) as when doing so in Spyder.

Related

Command Prompt / Anaconda Command Prompt not opening in Windows 11

I am doing a research project that involves me running terminals in the Anaconda Command Prompt. I was able to install and work the current version of Anaconda (4.12) and get the command prompt running there, but I had to depreciate my version of Anaconda to 4.2 so I can work on my project using Python 3.5. That version of Anaconda could not open the Anaconda Command Prompt or the Anaconda Navigator on my machine, so I tried reinstalling the current version of Anaconda after uninstalling 4.2; however, now I have the problem where the Anaconda Command Prompt can't open (but I've been able to run Anaconda Navigator). I tried doing the following things to see if I could fix this issue:
Opening the Anaconda Command Prompt through the Windows Command Prompt; however, now I have the added problem of the Windows Command Prompt not opening (which I tried fixing in the steps below)
Running the command prompt in Windows Safe Boot Mode. This method did open the command prompt, but it opened it up under the wrong letter drive. On this mode, I get the directory:
X:\windows\system32
What confuses me is that I don't even have an X: drive on my machine.
Trying to use some of the commands I was going to use in the command prompt in Powershell; however, when I opened powershell, I got an error message regarding Execution Policies. I was able to get the error fixed by changing some of the Execution Policies. I changed the execution policy on CurrentUser and LocalMachine to Unrestricted. This solved the problem on Powershell, but didn't fix the problem with opening the command prompt.
Going back into the Safe Boot Mode Command Prompt and seeing if the letter drive had been fixed to the default C:. The letter drive is still X:.
I don't know what to do to fix this problem. I've tried many of the things I read online from windows and other forums, but so far they haven't worked.
I had the same issue, here is the solution (extracted from Microsoft Assistance Service):
The fix was to open Registry Editor by searching "regedit" in the start menu, then search for "autorun" in the Edit tab, and delete the "autorun" file from the system through right click.
The offending "autorun" element is located under the key:
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
and contains an errorneous value if exists which prevented cmd.exe from starting properly.

Python3 installed, but why can't I run python file in terminal

I am trying to run a python3 file out of the directory the file is located in on macOS catalina.
so I type the following:
python3 <file_name>
and nothing happens in my terminal, it just starts a new line in the same directory, but no file runs.
Up until now I have only ever used a jupyter notebook to run python, but now I am trying to run VS code and run the file inside of the terminal.
Anyone know how to fix?
I just wanted to say I fixed the problem trying a number of different solutions:
I downloaded the most recent version of python, ran certificates and update commands.
I selected the new python interpreter from VS code using shift+cmd+P and select interpreter option.
I ran the file in python launcher and then again in terminal and it works fine now. Thanks for participating in helping me solve this issue.
Alternatively if anyone else has similar problems, try the steps I mentioned, or maybe try setting up a pyenv to run python files in a virtual python environment.

Why am I unable to run a program on the Anaconda Prompt?

I have a Python file called log_reg that I'm just trying to run in the anaconda prompt. I use the following commands:
cd Desktop
cd ML_models
python log_reg.py
I hit enter, and I don't get and error, but I also don't get any result or the file I'm trying to run. What should I do?
You should try running your code from the command line and see if it works. Go to the command line and type python log_reg.py and see if it runs. If so you'll know your code is correct. Then you can try running it at the Anaconda prompt.

How does one connect a Jupyter Kernel to VS Code if one does not connect automatically?

I want to be able to have a working jupyter notebook working for VS code out of the box, with minimal work on my side.
I tried opening a jupyter notebook. I immediately found the interpreter and used my conda env with the command pellet (command + shift + P then in the drop down menu found my conda env). This seems to make the terminal work since which python points to the right place:
(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ which python
/Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python
also running python scripts from VS Code seems to be working fine too, see output:
(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ /Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python /Users/brandomiranda/automl-meta-learning/python_playground.py
x = 1
my_str = this is a string
y = 2
but when I try the jupyter notebook it doesn't work.
Most noticeably my VS code does not have a kernel connected, look at the screenshot:
I tried clicking on a couple of arrows as suggested on reddit (vs_code_jupyter_server_no_kernel_python_not):
When I try running things in my jupyter notebook I get the following error:
Error: Activating Python 3.7.6 64-bit ('base': conda) to run Jupyter failed with Error: StdErr from ShellExec, /Users/brandomiranda/.bashrc: line 31: jump-module.bash: No such file or directory
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
.
which is strange. My shell is not even bash so I don't know why that message is being shown to me (maybe VS Code doesn't know Mac OS Catalina uses zsh? not sure how to fix that for VS Code). Regardless, I proceeded to do what I think the error message is suggesting. So I did the following:
I ran conda init and then went down to the terminal inside vs code and restarted the shell by running zsh but the jupyter kernel still doesn't work on my notebook in vs code. Not sure what's wrong.
In addition, I did what the error message suggested:
(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ conda init zsh
no change /Users/brandomiranda/miniconda3/condabin/conda
no change /Users/brandomiranda/miniconda3/bin/conda
no change /Users/brandomiranda/miniconda3/bin/conda-env
no change /Users/brandomiranda/miniconda3/bin/activate
no change /Users/brandomiranda/miniconda3/bin/deactivate
no change /Users/brandomiranda/miniconda3/etc/profile.d/conda.sh
no change /Users/brandomiranda/miniconda3/etc/fish/conf.d/conda.fish
no change /Users/brandomiranda/miniconda3/shell/condabin/Conda.psm1
no change /Users/brandomiranda/miniconda3/shell/condabin/conda-hook.ps1
no change /Users/brandomiranda/miniconda3/lib/python3.7/site-packages/xontrib/conda.xsh
no change /Users/brandomiranda/miniconda3/etc/profile.d/conda.csh
no change /Users/brandomiranda/.zshrc
No action taken.
but it seems it made no difference.
Any idea how to fix this?
I was suggessted by the developers of the VS code python extension to follow whatever they did here:
https://github.com/microsoft/vscode-python/issues/9566
but I can't figure out exactly it is they want me to do.
How do I fix this?
Related resources:
reddit: https://www.reddit.com/r/vscode/comments/eq2bfv/vs_code_jupyter_server_no_kernel_python_not/
gitissue: https://github.com/microsoft/vscode-python/issues/9636
I initially thought it was a conda issue so I looked into this question: Activate conda environment stopped working in vscode
https://www.reddit.com/r/vscode/comments/eshxka/how_does_one_connect_a_jupyter_kernel_to_vs_code/
https://www.quora.com/unanswered/How-does-one-connect-a-Jupyter-Kernel-to-VS-Code-if-one-does-not-connect-automatically
You need to select the python interpreter for jupyter, you can do it by following the step
Open command panel
Mac: CMD+Shift+P
PC: CTRL+SHIFT+P
Then search for select Interpreter to start jupyter server then hit enter, it will list all the interpreter, then select any interpreter and done!
it's just a one time process, after this, it will get connected automatically.
As bizarre as it seems, I also noticed this the other day and the only thing that works so far for me is to open VS Code by launching it from the Anaconda Navigator:
Then I get, as expected:
If you see the very long discussion I had in the git issue (https://github.com/microsoft/vscode-python/issues/9566) once I removed all the errors thrown by my .bashrc and .zhrc, the jupyter feature in VS code started working for me again. It's super weird (specially because I am NOT using bash at all and I am using zsh as my shell, I would have expected VS code to be robust to my .SHELLrc files throwing errors but it's not).
If that doesn't work, then you might have to install the vsix view extension and install the ms-python-insiders.vsixlinked in the issue (https://github.com/microsoft/vscode-python/issues/9566).
If on Windows VSCode, what worked for me was installing and enabling the Python and Jupyter extensions, then CTRL+SHIFT+P, select Interpreter to start jupyter server. Those extensions were the bottleneck.

Running python on Command prompt:Access is Denied

I installed python long back and worked fine for all these months, I could install libraries from command prompt..run python programs using shell from vba until one day..It started failing, I am able to run python scripts from idle but not from command prompt or vba. I have tried checking my permission but to no success.
If I type python on command prompt, it says the app can't run on this pc and later "Access Denied" displays on the prompt.
To someone who might find this: I had the same problem, when I checked the python.exe file located at C:\Users{your user}\AppData\Local\Programs\Python\Python37 it had 0 bytes, I just reinstalled it to solve the problem, I don't know how this happened.

Categories