Python Modules are not being recognized by Bash (Cygwin) - python

I'm trying to run a python script in bash (with Cygwin on Windows 10), however it does not recognize modules like Matplotlib. For example, if I want to compile and run a script, I type in python text_reader.py and the result is ModuleNotFoundError: No module named 'matplotlib'. The script works fine in the CMD terminal and in Pycharm, but not in BASH. I believe the issue is that the the path of the python interpreters are different. If so, then how could I make the paths of the interpreters the same?

you need to install it most likely. have you tried.
pip install matplotlib
OR
pip3 install matplotlib

I have found the solution. I had to change the PATH environment variable to my python interpreter with the following BASH command,
$ export PATH="PATH TO PYTHON INTERPRETER"
and then I can normally compile a python file using a numpy or matplotlib module with
$ python text_reader.py

Related

I keep getting "ModuleNotFound" error when i try to run code on my terminal even though i installed it

i work with:
Atom as a text editor
GitBash as a terminal
i've been trying to work with the numpy module but, even though i installed it via conda command on my terminal, it still gives me the ModuleNotFoundError when i run my code on the terminal
For example, here's some code:
https://i.stack.imgur.com/GL4JP.png
and here is me trying to run it on my terminal:
https://i.stack.imgur.com/ChOWm.png
As you have mentioned that you have installed numpy using conda, run your script or .py file in anaconda prompt (python yourfile.py) instead of GitBash or windows command prompt.
Otherwise, add python to the path and make sure that you are adding the right variable to path. If you are using conda installation of python, then add that to your path or if you installed python standalone, you should add that to path. Use following commands to know where is python installed in your system and add to path accordingly. Then use GitBash or CMD to run your files.
In CMD:
where.exe python
In GitBash:
which python
or
where.exe python

What does the term: Python -m mean?

I'm trying to run jupyter notebook.
For some reason, I get the error that jupyter is not a command :(
I looked at Running jupyter via command line on Windows user6094431's answer.
What does Python -m mean?
As in python -m notebook.
I used pip to install Jupyter.
"jupyter notebook" doesn't work btw.
The -m you're asking about is one of the command line flags that the Python interpreter recognizes. It tells the interpreter to take the next thing on the command line and treat it as a module name, like in an import statement. The named module will be used as the main module in the interpreter, like a script.
If you want to follow the advice of that answer, you need to go to an command line, and type in, literally python -m notebook. The first word is the program to run, the -m and notebook are arguments.
On Windows, the Python interpreter might not be in your PATH by default. If that's the case, you can instead try running py, which is a helper program that can run any installed Python interpreter. If you have more than one interpreter installed, you might need to pass a flag telling py which version you want (e.g. py -3.7 -m notebook). Extra arguments to py will be passed on to the Python interpreter, so -m should work fine.

Pip not installing Python modules to proper directory

So I downloaded a module, but when I tried to use it in a program, Python threw the error:
"ImportError: No module named markovify"
When I go to try to change my .bash_profile, it shows this:
"# Setting PATH for Python 3.5
The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH"
I've been using 2.7.10 all this time, and Terminal defaults to it. (When I type in Python, that's the version it says I'm using)
I'm trying to get pip to install modules for python 2.7.10 to a different folder.
What's weird is this: I seem to have installed pyparsing with pip and it seems to work. I tried installing markovify and it throws this error message.
What am I missing?
When I go to try to change my .bash_profile, it prints this
That suggests you're using some tool to change your profile. Did you also use that tool to install markovify? You need to be clear about your environment when you have environment questions! :-)
If you installed markovify from the command line, I'd suggest opening a new terminal and running these commands, just to make sure you're working from where you think you are:
$ python --version
Python 2.7.10
$ command -v python
/usr/bin/python
The most likely problem, it seems to me, is that you got a bit tangled up and wound up doing something other than what you intended. From a fresh start, provided you're starting with the environment you want, pip should, you know, Just Work.
I'm trying to get pip to install modules for python 2.7.10 to a different folder.
That's possible to do, but are you sure that's what you want? Usually if you're using the Python interpreter you intend to, and haven't putzed with PYTHON_PATH & friends, when you run pip, it will install to its default location, which sure enough is where Python will look for it when you say to import it.
As an experiment, you might check if markovify was somehow installed for Python 3.5. Try
$ /Library/Frameworks/Python.framework/Versions/3.5/bin/python -c 'import markovify'
(You might also want to check that the Python interpreter is in fact in that folder and has that name.)
When you're working on more than one Python version, you should work on Virtual Environments:
Virtual Env on Hitchhiker's Guide

Running python files from command prompt

I have written a python program in eclipse that imports the mechanize module. It works perfectly there. When I run the .py file from the command prompt, it shows this error: "No module named mechanize". How do I rectify this?
Make sure that Eclipse and prompt are using the same python version. Simply typing $ python on the command line show you the version you are using from there.
The mechanize module must be in your site-packages folder in order for python to find it.
(C:\Python\Lib\site-packages)
If the module is not in your site-packages folder then you can install it as follows:
Download the source code from http://pypi.python.org/packages/source/m/mechanize/mechanize-0.2.5.tar.gz
Now extract and install the package (This is what you do on Linux, on Mac or Win. this might be slightly different)
$ tar zxvf mechanize-0.2.5.tar.gz)
$ sudo python setup.py install

Python ImportError while module is installed [Ubuntu]

I'd like to make a switch from Windows to Linux (Ubuntu) writing my python programs but I just can't get things to work. Here's the problem: I can see that there are quite the number of modules pre-installed (like numpy, pandas, matplotlib, etc.) in Ubuntu. They sit nicely in the /host/Python27/Lib/site-packages directory. But when I write a test python script and try to execute it, it gives me an ImportError whenever I try to import a module (for instance import numpy as np gives me ImportError: No module named numpy). When I type which python in the commandline I get the /usr/bin/python path. I think I might need to change things related to the python path, but I don't know how to do that.
You can use the following command in your terminal to see what folders are in your PYTHONPATH.
python -c "import sys, pprint; pprint.pprint(sys.path)"
I'm guessing /host/Python27/Lib/site-packages wont be in there (it doesn't sound like a normal python path. How did you install these packages?).
If you want to add folders to your PYTHONPATH then use the following:
export PYTHONPATH=$PYTHONPATH:/host/Python27/Lib/site-packages
Personally here are some recommendations for developing with Python:
Use virtualenv. It is a very powerful tool that creates sandboxed python environments so you can install modules and keep them separate from the main interpreter.
Use pip - When you've created a virtualenv, and activated it you can use pip install to install packages for you. e.g. pip install numpy will install numpy into your virtual environment and will be accessible from only this virtualenv. This means you can also install different versions for testing etc. Very powerful. I would recommend using pip to install your python packages over using ubuntu apt-get install as you are more likely to get the newer versions of modules (apt-get relies on someone packaging the latest versions of your python libraries and may not be available for as many libraries as pip).
When writing python scripts that you will make executable (chmod +x my_python_script.py) make sure you put #!/usr/bin/env python at the top as this will pick up the python interpreter in your virtual environment. If you don't (and put #!/usr/bin/python) then running ./my_python_script.py will always use the system python interpreter.
/host/Python27/Lib/site-packages is not a default python directory on linux installations as far as I am aware.
The normal python installation (and python packages) should be found under /usr/lib or /usr/lib64 depending on your processor architecture.
If you want to check where python is searching in addition to these directories you can use a terminal with the following command:
echo $PYTHONPATH
If the /host/Python27/Lib/site-packages path is not listed, attempt to use the following command and try it again:
export PYTHONPATH=$PYTHONPATH:host/Python27/Lib/site-packages
If this should work and you do not want to write this in a terminal every time you want to use these packages, simply put it into a file called .bashrc in your home folder (normally /home/<username>).
When installing other python libraries, specify the pip version you want to install it to, if it's python2 you use, then enter this syntax:
pip2 install <package>
For python3
pip3 install <package>

Categories

Resources