I had the following issue that was giving me a lot of troubles. I managed to solve it after 2 1/2 hours and to spare some poor soul the same waste of time I wanted to show how I resolved it.
Loading a python file inside a .bat file was usually working quite well. However, I encountered issues when I tried to import pandas.
Code could look like this
import pandas as pd
print ("hello")
and the following result in the cmd prompt would be
ImportError: Unable to import required dependencies:
numpy:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
My .bat file would look like
#echo off
"C:\Users\myUserName\Anaconda3\python.exe" "C:\path to .py file\MyPythonFile.py"
pause
To solve this I tried a variety of things like playing around with paths within windows and a large variety of other things.
After I opened python.exe within the Anaconda3 folder, I received
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation
I found myself unable to resolve this within the command prompt but I managed to finally understand the core problem. With Anaconda3 being unactivated, it would never import pandas as intented while other imports were working as intented.
The solution that ended up working was adding the path of the activate.bat file inside the Anaconda3 folder. So the final .bat file would look like
#echo off
call "C:\Users\myUserName\Anaconda3\Scripts\activate.bat"
"C:\Users\myUserName\Anaconda3\python.exe" "C:\path to my Python Script\MyPythonFile.py"
pause
In a ideal scenario, I would be able to keep it activated but calling it within the .bat file is a adequate enough solution for me and may be enough for you too.
Related
I am new to Python so this may be a stupid question, sorry in advance if it is so!
The paths Python searches for modules using Spyder 4.1.4.:
[.., 'D:\Users\Martin\Anaconda3\lib\site-packages', ..]
The paths Python searches for when used via command line in windows:
[.., 'D:\Users\Martin\Anaconda3\lib\site-packages', ..]
Thus, both include path to
'D:\Users\Martin\Anaconda3\lib\site-packages'
where I store my cv2 module installed. Yet, Spyder can import cv2 and to my surprise cmd cannot import cv2 with an error No module named 'cv2'. What am I missing please? Can anyone verify if it works for him in command line please? (I changed/edited the question again to cut unneccessary long talk, though the essence stayed the same, in hope someone could answer).
I solved it. If you have a similar problem, you can check if your case is similar to mine and follow the steps.
I use Spyder and everything works fine. However my goal is to use Python via cmd. This could not be done because Python via cmd could not find my modules.
What I did: After typing python in cmd, Python worked and output its version by default. However, I noticed the version was different than version running in Spyder - I had two Python installations (one of which I didn't know about). I found the installation cmd uses by import sys and print(sys.executable). I uninstalled that Python installation.
Now, typing python in cmd does not do anything, it is an unknown command. Then I followed this guide to add my Python installation to Path (using Windows) by:
My Computer > Properties > Advanced System Settings > Environment Variables >
in System variables, I found "Path", clicked Edit, New, then input my Python installation path D:\Users\Martin\Anaconda3 as well as D:\Users\Martin\Anaconda3\Scripts and D:\Users\Martin\Anaconda3\Library\bin. This solved it for me.
I recently updated Anaconda (Spyder (Python 3.6), Windows 10). Several of my libaries disappeared and had to be re-installed. However, I have been unable to reinstall scipy. When I run "conda install scipy -f" from the Windows PowerShell, scipy seems to be reinstalled. However even if I close Spyder and restat Windows, I continue to get the error: ModuleNotFoundError: No module named 'scipy'.
I know that problems similar to this one have been posted previously, but I have been unable to find anything that seems to fix the problems.
Thanks in advance for any advice (and apologies if this question was posted yesterday as well).
I battled with this same issue. The reason is most likely because you already have a file in the current directory called scipy.py. For example, when I first started with scipy, i made a file literally called scipy.py to practice in. Then, when I tried to import scipy from a different file, the first instance of 'scipy.py' was my practice folder, rather than the actual package which was further into the directory.
I advise you check for folders that you may have named scipy.py that aren't the actual scipy package. I could be wrong, but it seems likely that python will first search the folder it saves your files to, and then trundle over to the packages folder saved elsewhere.
Hopefully this helps some other folks who are stumped by the same issue!
I'm fairly new to IDE's and I'm trying to take courses in Python. No matter what I try, I cannot successfully run a python script that has import pandas and import numpy in it in either Visual Studio Code or Eclipse (running on Windows 10). I have Python 3.8 installed, and when I try running those commands in the shell it works fine. I suspect when I try executing an actual Python script instead of using the console, it might be using a different interpreter, and I only get errors when I try doing this, saying numpy is not defined. I also get the error "cannot import name 'numpy' from partially initialized module 'pandas' (most likely due to a circular import)" when I specify "from pandas import numpy" rather than "from pandas import *".
I am very frustrated and don't know how to fix this problem. I've tried searching for help but not having a programming background, I don't know where to go to resolve this or how.
I also cannot get pip or pip3 to work at all to install packages. Those commands don't get recognized.
Please help!
I recommend using Jupyter Notebooks/pycharm(IDE). Both are very useful for learning python and working with data, data manipulation, and data visualizations.
PyCharm knows everything about your code. Rely on it for intelligent code completion, on-the-fly error checking and quick-fixes & easy project navigation.
While
Jupyter Notebooks can run line by line, rerun specific lines after making changes, and it's inline output is very useful for debugging and visualizations. You can get it from https://jupyter.org.
Zepellin Notebooks can also serve as alternatives.
When trying to run my code in Pycharm i get this error:
from .gurobipy import *
ImportError: libgurobi80.so: cannot open shared object file: No such file or directory
In the regular python terminal i can import gurobipy with no problem. How can Pycharm be fixed?
I've read the install instructions and posted the following 3 lines at the end of the bashrc file with a text editor.
export GUROBI_HOME="/opt/gurobi800/linux64"
export PATH="${PATH}:${GUROBI_HOME}/bin"
export LD_LIBRARY_PATH="${GUROBI_HOME}/lib"
Reinstalling PyCharm (even switching to EDU and Professional) as well as reinstalling Ubuntu did not change the error. Please help.
I had the same error and configured PyCharm on Kubuntu by adding the environment variables to the run config.
Select Edit configurations... and add the variables to the Environment variables field. In my case, I added GUROBI_HOME, LD_LIBRARY_PATH and GRB_LICENSE_FILE. Also, I had to enter the values directly: LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GUROBI_HOME/lib didn't work, but LD_LIBRARY_PATH=/path/to/gurobi/lib works.
Code completion and suggestions still don't work, but at least I can run from PyCharm now.
I went around this problem by just editing the file in PyCharm and running it via the terminal. I didn't find another solution so far, so I'm still proceeding this way. It seems to be a PyCharm-specific problem.
I tried more than 10 ways to install Gurobipy in Pycharm. However, there is no way to use.
There are two days to use Gurobipy
Jupyter based on Conda - installation : recommended by Grobipy.
Install Grobipy indirectly to python (This Is what i did for my setting)
Hope Pycharm and Gurobipy to work together to resolve this issue. But most people might use Jupyter these days. So don’t know how much they will be interested in fixing this.
I do research at my school by sshing into one of the machines in our lab. I do not have root access to these machines, so when I need to install new Python modules I typically build them in a local directory and modify my PYTHONPATH to point to this directory, rather than installing them to the standard directories (which I don't have permissions to). This has worked fine for several packages in the past, but with Numpy running
python setup.py install
seems to work, but there is no __config__.py file in the built directory. (The one it's supposed to import the module from) This causes the line in __init__.py
from numpy.__config__ import show as show_config
to raise an ImportError. I can't figure out why __config__.py isn't being generated as Numpy seems to assume that it will be. Running the build script seems to work; it completes with no troubling error messages. I've looked but can't find any answers to this. Please help.
I am using Python 2.6.2.
Well, I got numpy working by setting PYTHONPATH to point to the build/libXXX folder, but I was doing so to install scipy, which still demands some files as dependencies that don't exist in the either build directory I was trying. But that's another issue.