Installing & running modules in Python 3 (Beginner) - python

I am very new to the world of coding, so I will try to provide as much information as i can regarding to my question.
Essentially, I wanted to install a module (moviepy) for Python 3. The site were I found the module suggested I use pip to unpack and install the module, so I did.
In my terminal, I entered pip install moviepy and pip proceeded to unpack and install my module, yay!
I then went over to my IDLE to see if the module would import, import moviepy, but received this error:
ImportError: No module named 'moviepy'
Huh? I thought I had just installed moviepy?
Upon further investigation, the module appears to have been written to my Python 2.7 site-packages folder and not my in Python 3 site-packages folder.
So my question is: How can I get my module to install to Python 3?
The modules website says that it is compatible with Python 3.
Im assuming this is a file path issue of some kind, but i don't know where to begin.
I'm currently using a OS X Yosemite version 10.10.2, Python 2.7.6, Python 3.5.0
Any help or comments are greatly appreciated here!
Help the n00b!

If you are using python in linux you must run pip with python3:
python3 -m pip install moviepy
according to python official doc :
On Linux, Mac OS X and other POSIX systems, use the versioned Python
commands in combination with the -m switch to run the appropriate copy
of pip:
python2 -m pip install SomePackage # default Python 2
python2.7 -m pip install SomePackage # specifically Python 2.7
python3 -m pip install SomePackage # default Python 3
python3.4 -m pip install SomePackage # specifically Python 3.4

Since pip itself is written in python , you could simply run the following in your terminal:
/path/to/python3 /usr/bin/pip install foo
More info:
To install pip, securely download get-pip.py
Run the following (which may require administrator access):
/path/to/python3 get-pip.py

Try below version of moviepy
pip install moviepy==0.2.3.5

Related

How do I deal with pip in different python versions?

I had python 3.8 installed and then I installed python 2.7. I am trying to run a python program with py -2 program.py in vs code using with python 2.7 as selected environment and I am getting an error, ImportError: No module named googlemaps even though I have already installed.
If I run the program using Python3 then it would run fine. Also when I open vs code using python 2.7 as selected runtime environment then I would get a warning Linter Pylint is not installed. If I click on install then I would get another warning There's no Pip installer available in the selected environment.
Also even though I have changed the python path from 3.7 to 2.7, default python version will still show up as 3.7 when I runPython in command line.
Things that I have tried to install the googlemaps module for python 2 after googling for solutions,
pip2 install googlemaps--upgrade
py -2 -m pip install googlemaps
If you have your python2 binary located, you can just call it directly:
/usr/bin/python2 -m pip install googlemaps
And if you're not sure where your python binary is, you can use
import sys
print(sys.executable)
to locate it.
And if you don't have pip, you should install it by downloading this file:
https://bootstrap.pypa.io/get-pip.py
then running:
/usr/bin/python2 get-pip.py
It is recommended to install Python 3.8 using Pyenv and also when you are using different versions of python, it is very useful
curl https://pyenv.run | bash
pyenv install 3.8.1
pyenv virtualenv 3.8.1 venv
pyenv local venv
with pyenv local you set your version for use.
If after this you run
pyenv version
It will output to 3.8.1
With regards to pip installation, run
whereis python
and if it outputs to
usr/bin/python2
then you can use pip for installing python2 packages and pip3 for packages compatible to python3.

How to use pip on a specific version of python?

So my problem is that I have two versions of python: 2.7 and 3.2. I want to install pycrypto on the 2.7 one but when I try to do this:
python2.7 pip install pcrypto
Or:
pip2.7 install pycrypto
It says that python2.7 or pip2.7 is not a recognized command.
What should I do?
pip installs a library for any version of Python 2
pip3 installs a library for any version of Python 3
On linux terminal, type:
whereis pip
It will print all available paths to pip installations. Copy-paste the full path to the desired pip, e.g.:
/usr/local/bin/pip2.7 install pcrypto
If you want to use pip of a specific python interpreter you can use this interpreter with the -m option to use the specific pip.
python -m pip ...
... should be replaced with the desired pip commands.
The -m option allows to run a library module as a script (in this case the associated pip)

Python 2.7: No module named setup tools

I am trying to clone and install the keras package from github. When I run sudo python setup.py install, it says no module named setuptools. I looked at some posts that were for python 3; however, this solutions did not work for me. I am working on a terminal from a windows computer. I am using python 2.7. I tried the following commands: python -m pip install -U pip setuptools from this website: https://packaging.python.org/installing/. However, nothing seemed to work.
If you are working on Windows then you should be typing batch commands, not bash (Linux, OSX), therefore, sudo ... should not be used by you.
Firstly, I would recommend to install pip, it will make package update and installation a lot easier. Save it as get-pip.py and run the following in the directory the file is located python get-pip.py. Official pip website.
Then install upgrade pip which will also install the latest versions of setuptools and wheel with this python -m pip install -U pip setuptools.

Using pip on Windows installed with both python 2.7 and 3.5

I am using Windows 10. Currently, I have Python 2.7 installed. I would like to install Python 3.5 as well. However, if I have both 2.7 and 3.5 installed, when I run pip, how do I get the direct the package to be installed to the desired Python version?
You will have to use the absolute path of pip.
E.g: if I installed python 3 to C:\python35, I would use:
C:\> python35\Scripts\pip.exe install packagename
Or if you're on linux, use pip3 install packagename
If you don't specify a full path, it will use whichever pip is in your path.
Because usually i change my intepreter to run something(i got 2 diff projects with both 2 and 3), i use these solution:
Add path to the environment as usual (of course)
Rename ur python.exe , in my case i want to run python 3 using command python3 on my cmd. So i renamed my python.exe in python3.x directory with python3. Itll works with python 2 ofc.
Then to use pip in both python, i use this command.
python3 -m pip install 'somepackage'
and to run pip on python2
python -m pip install 'somepackage'
This is may not the best solution out there, but i like this one
** WINDOWS **
ref : https://datascience.com.co/how-to-install-python-2-7-and-3-6-in-windows-10-add-python-path-281e7eae62a
In my case, I have Python 2.7 and Python 3.4, with the Python Launcher for Windows.
This is the output when running this commands:
PS C:\> pip -V
pip 9.0.1 from c:\python27\lib\site-packages (python 2.7)
PS C:\> pip3 -V
pip 9.0.1 from C:\Python34\lib\site-packages (python 3.4)
I'll note that in my Python27\Scripts\ directory, I have pip.exe, pip2.exe and pip2.7.exe.
And in my Python34\Scripts\ directory, I have pip.exe, pip3.exe and pip3.4.exe.
So all of these .exe files help you when you have different versions of Python installed at the same time.
Of course, for this to work, you have to have the respective Scriptsdirectries in your Path system enviroment variable.
The answer from Farhan.K will work. However, I think a more convenient way would be to rename python35\Scripts\pip.exe to python35\Scripts\pip3.exe assuming python 3 is installed in C:\python35.
After renaming, you can use pip3 when installing packages to python v3 and pip when installing packages to python v2. Without the renaming, your computer will use whichever pip is in your path.
I would advise against ever calling any pip script directly (nor pip3, pip2.7.exe, anything like that).
Instead, a surefire way is to always prefer the explicit variant of calling pip's executable module for a specific Python interpreter:
path/to/pythonX.Y -m pip somecommand
path/to/venv/bin/python -m pip somecommand
C:\path\to\venv\Scripts\python.exe -m pip somecommand
There are many advantages to this, for example:
It is explicit for which Python interpreter the projects will be pip-installed (Python 2 or 3, inside the virtual environment or not, etc.)
For a virtual environment, one can pip-install (or do other things) without activating it: path/to/venv/bin/python -m pip install SomeProject
Under Windows this is the only way to safely upgrade pip itself path\to\venv\Scripts\python.exe -m pip install --upgrade pip
But yes, if all is perfectly setup, then python3 -m pip install SomeProject and pip3 install SomeProject should do the exact same thing, but there are way too many cases where there is an issue with the setup and things don't work as expected and users get confused (as shown by the many questions about this topic on this platform).
References
Brett Cannon's article "Why you should use python -m pip"
pip's documentation section on "Upgrading pip"
venv's documentation section on "Creating virtual environments": "You don’t specifically need to activate an environment [...]"
I ran across an issue with running pip with absolute path. This might be related to WinPython's installation routine and the order of installing Python 3.6 first, 2.7 second, or Python 3.6 being in the path.
No matter which pip was called, it was activating the 3.6 one:
λ C:\prog\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64\Scripts\pip2.exe --version
pip 9.0.1 from C:\prog\WinPython-64bit-3.6.1.0Zero\python-3.6.1.amd64\lib\site-packages (python 3.6)
What finally did the trick was calling pip as a module of the respective python binary:
λ C:\prog\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64\python.exe -m pip --version
pip 9.0.1 from C:\prog\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64\lib\site-packages (python 2.7)
Hope that might help someone with similar issues.
I tried many things , then finally
pip3 install --upgrade pip worked for me as i was facing this issue since i had both python3 and python2.7 installed on my system.
mind the pip3 in the beginning and pip in the end.
And yes you do have to run in admin mode the command prompt and make sure if the path is set properly.
1-open command prompt and change direction using the command cd C:\Python35\Scripts
2- write the command pip3 install --upgrade pip
3- close the command prompt and reopen it again to return to the default direction and use the command pip3.exe install package_name to install any package you want

"no module named PyPDF2" error

I use Spyder, with Python 2.7, on a windows 10. I was able to install the PyPDF2 package with a conda command from my prompt. I said installation complete. Yet, If I try to run a simple import command:
import PyPDF2
I get the error:
ImportError: No module named PyPDF2
How can I fix this?
In my case, I was trying to import 'pyPdf2' instead of 'PyPDF2'. Observe the case.
import PyPDF2
is correct.
If you use python3 maybe
apt-get install python3-pypdf2
I faced the same problem. But, In my case,
I previously installed Python3 separately from official website and was using without any issues
Then later I installed Anaconda package distribution software which itself has another Python3 installed in corresponding directory.
So, when I installed PyPDF2, it installed normally and while importing throws an error, because the base path of python3 was changed to be used with Anaconda.
Then I opened Anaconda prompt and installed PyPDF2 there and tried to import. It worked!!
Then I can use it from any command prompt in my Windows PC. Or else you can delete Anaconda and everything works normally. Its just a conflict of two pythons in my pc.
Conclusion: Try any overlapping softwares in your PC(in my case Anaconda prompt) and try their CMD to install packages and import. If I wanted to install any package I have to go to Anaconda prompt and install it and importing that modules works anywhere without any error. So from now on wards I'm only using Anaconda prompt as my default installation prompt.
This is the case which I followed for python3. For python2 try with pip:
pip install PyPDF2
I had this problem too when I tried to import PyPDF2 like this:
sudo apt-get install python-pypdf2
When running some simple script with import PyPDF2, I would get an error like this:
ImportError: No module named PyPDF2
The solution was to also install pdfmerge, like this:
pip install pdfmerge
How to install Python packages on Windows, Mac, and Linux for various versions of Python which are simultaneously installed:
I have multiple versions of Python installed on my Windows 8.1 machine (Python 2.7, 3.5, and 3.7). This created problems (confusion, I should say). You must therefore be very explicit when installing packages. Ex:
py -3.7 -m pip install PyPDF2 # on Windows
python3.7 -m pip install PyPDF2 # on Mac and Linux
INSTEAD OF the more generic:
pip install PyPDF2 or
pip3 install PyPDF2
And to upgrade pip, be very specific in your python version, like this:
py -3.7 -m pip install --upgrade pip # on Windows
python3.7 -m pip install --upgrade pip # on Mac and Linux
INSTEAD OF the more generic:
py -3 -m pip install --upgrade pip # on Windows
python3 -m pip install --upgrade pip # on Mac and Linux
Now, I can run python 3.7 with py -3.7 on Windows, or with python3.7 on Linux, and since I did py -3.7 -m pip install PyPDF2 on Windows, or python3.7 -m pip install PyPDF2 on Linux or Mac, the import PyPDF2 command works! Previously, since I had only done pip3 install PyPDF2, the import PyPDF2 command only worked if I ran py -3.5 on Windows or python3.5 on Linux, oddly enough, since apparently that was my "default Python3 version" which the more generic pip3 install PyPDF2 command must have installed the PyPDF2 module into. I think it has something to do with the fact that I installed Python 3.5 for all users, but Python 3.7 for only my user account, so the different pip install commands were placing the installed packages into different locations, with the 3.5 version being the "default" Python3 install location.
See more here: https://docs.python.org/3/installing/index.html:
... work with multiple versions of Python installed in parallel?
On Linux, Mac OS X, and other POSIX systems, use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip:
python2 -m pip install SomePackage # default Python 2
python2.7 -m pip install SomePackage # specifically Python 2.7
python3 -m pip install SomePackage # default Python 3
python3.4 -m pip install SomePackage # specifically Python 3.4
Appropriately versioned pip commands may also be available.
On Windows, use the py Python launcher in combination with the -m switch:
py -2 -m pip install SomePackage # default Python 2
py -2.7 -m pip install SomePackage # specifically Python 2.7
py -3 -m pip install SomePackage # default Python 3
py -3.4 -m pip install SomePackage # specifically Python 3.4
When using pip, it usually gets installed in Python 2+ so try
pip3 install PyPDF2
I had the same issue and fixed it when switching Python compiler (bottom left corner on Visual Studio Code) . Try on different versions and eventually it should work.
Im following a UDEMY course here. Im using Anaconda prompt and jupyter notebook.
I encountered the same issue as OP. What I did to have the library working:
restart the environment
go to your anaconda prompt
control c to stop the running instance
conda activate ***your_env_here***
pip install PyPDF2
(in my case open the jupyer notebook) jupyter notebook
You can now import the library without the error. import PyPDF2
Hope this works for you.
I encountered the same issue today while doing Udemy course.
try the following:
type this
import sys
!{sys.executable} -m pip install PyPDF2
then
import PyPDF2
Hope it works for you too.

Categories

Resources