Running python files from command prompt - python

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

Related

Python Modules are not being recognized by Bash (Cygwin)

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

Pyinstaller command not found (MacOS)

I've been trying to use PyInstaller on my program, scratch_1.py. The PyCharm project folder is called "idigen", which is saved in my desktop. So, I changed the director like so:
cd /Users/joelsaarinen/Desktop/idigen
then, moved on to use pyinstaller, and I get this error:
pyinstaller scratch_1.py
-bash: pyinstaller: command not found
I'm confused because when I use:
pip show pyinstaller
to verify that I have pyinstaller installed, it returns a positive result.
Is there an additional command I should be putting in when using Pyinstaller on one of my files? Might this be an issue with this specific program or the operating system in general? Thanks in advance.
This is a common problem due to the fact that you might install a different version of python and keep using an old version that is preinstalled in the machine. Here is the best solution.
First, check the version of the python that you installed. In my case, i installed python 3.5 and the machine had python2.7. If you run python on the terminal, most likely the preinstalled one is the one that will run.
Second, check the directory of your desired python version. watch -a python3 is the command to run to see your directory.
Third, set the directory as the main one to run your python commands.
alias python=/usr/local/bin/python3 does the whole trick
Lastly, reinstall pip. Download the get-pip.py file and run sudo /usr/local/bin/python3 get-pip.py * I used the path to show the reason we updated the alias*
Now you can run pyinstaller without problems
pyinstaller appears to have installed correctly, but the command is not available on PATH. You need to locate where the executable was placed.`below to find executables
set | grep pyinstaller
now modify path by this
export PATH=some_path:another_path
launchctl setenv PATH $PATH
I just downloaded the source code of pyInstaller from official website, put it where I can find it and wrote a script which launches pyinstaller.py from that folder.
For some reason, pyinstaller.py is missing in the pyInstaller installation downloaded via pip.
I had the same issue on MacOS with Developer Tools 11.4 and found two ways to start pyinstaller:
alt 1: path based solution
$ pip3 show -f pyinstaller|grep pyinstaller
will find pyinstaller in a bin path:
../../../../usr/local/bin/pyinstaller
...
So you can use one of the set-the-path-or-an-alias approaches or call via fully qualified path.
alt 2: call via python module
$ pip3 show -f pyinstaller|grep __init__
will get you a hint on how pyinstaller is defined as a module:
PyInstaller/__init__.py
...
With that capitalization, it's possible to call pyinstaller as a module with the following:
$ python3 -m PyInstaller --version
4.2
I'm using the latter now.

python unable to use .whl files

I am trying to install pymedia, but I can't. I know I need pip, but I can't get pip. on every tutorial, it always says to do
python -m ensurepip
but for me it always just says
'python' is not recognized as an internal or external command,
operable program or batch file.
I have windows 7 64 bit and python 2.7
It seems that python is not on your path.
Check if the python directory with python.exe is listed in the environment variable PATH. If not, just add it and try again.
You should be able to start python shell by running python in command line.
You have 2 easy options. First you need to be running pip from your scrips folder in the python directory.
Download your whl file and place it in the scripts folder where your pip.exe is.
In command prompt (windows) type:
#note the exact file path may be different but should look something like this.
cd C:\program files\Python\Scripts
after you are in your scripts folder in command prompt you can then run the command:
pip install nameofpackage.whl
note if you have more than one python version installed make sure you are running pip in the correct directory and if you have 2.X and 3.X installed you may want to use pip3 instead of pip
For windows users your second option here:
like the first option make sure you are in your scripts folder in command prompt then you can run a command to install your whl file on a local directory:
pip install C:/path/to/dir/with/filename.whl

Why isn't UniCurses for Python working?

I'm trying to make my first UniCurses project with Python on OpenSUSE.
I put the import statement in my .py file, but when I tried to run it, it says the module is not there... So I downloaded UniCurses from the website, and the instructions say Unix's Python already has UniCurses. That's odd, but I continued. I put the downloaded unicurses.py into my project directory, and when I tried running my file, an error message says UniCurses is not compatible with my system, and that either my Python distribution is below v2.6 or my operating system is something other than Windows or a *nix. My Python is v2.7.8, and again, my OS is a Linux distro. Why is this happening, and what should I do?
Edit: It's worth noting that the regular curses supposedly doesn't work on my system either.
Answer by Sagar Rakshe from How to install Python package from GitHub:
To install Python package from github, you need to clone that repository.
git clone https://github.com/jkbr/httpie.git
Then just run the setup.py file from that directory,
sudo python setup.py install
If you have already downloaded the file you can skip the first step and just run the python setup.py install in the folder. (I don't think sudo is necessary for python)

How to install wexpect?

I'm running 32-bit Windows XP and trying to have Matlab communicate with Cgate, a command line program. I'd like to make this happen using wexpect, which is a port of Python's module pexpect to Windows. I'm having trouble installing or importing wexpect though. I've put wexpect in the folder Lib, along with all other modules. I can import those other modules but just not wexpect. Commands I've tried include:
import wexpect
import wexpect.py
python wexpect.py install
python wexpect.py install --home=~
wexpect install
Does anyone have anymore ideas?
If you installed wexpect somewhere in the module search path (sys.path), then import wexpect is what you would use. You have to make sure you installed it in the right path, though (the usual location is Lib\site-packages inside the Python installation.) If the package wexpect.py was in came with a setup.py file, you could install it with
\path\to\python setup.py install
from a DOS prompt. Depending on what else you did you may need to restart the Python interpreter or IDE that you're using for it to pick it up. You should also pay attention to the errors you get when you try import wexpect, as it may have dependencies you need to install.
Perhaps you haven't installed wexpect correctly.
In your command prompt (not python, but the DOS-like command shell), go to the directory where you downloaded wexpect. Make sure it's unzipped and you can see the setup.py file when you use the dir command.
Then enter the command (again, in the cmd shell, not the python terminal):
python setup.py install.
I have created a Github repo and PyPI project for wexpect. So now wexpect can be installed with:
pip install wexpect

Categories

Resources