Installing Python modules without command prompt. - python

I have Python on my school computer, but I would like to install a few modules. How could I get passed this problem.

You can try the following:
import subprocess
subprocess.call('pip install X --target=d:\some\folder\with\write\permission', shell=True)
Is a python script that will try to install the package X in some folder that you want. Maybe this way you can do it.

I installed python packages in school with this.
open a python shell and write the following:
import pip
pip._internal.main(["install", "--user", <PACKAGE>])

For school computers that don't allow you install libraries via cmd here's a way around it!
import pip
press enter
pip.main(["install", "--user", "Libraryname"])
"user" may not be important so only use it if it doesn't work the first time running it

I got around this problem by running the following:
import os
os.system('pip install --user X')
which did not require admin privileges.
You may then need to add the packages directory to the path

You can also install libraries from powershell if using a windows computer.
First open up IDLE
import subprocess
subprocess.call('powershell.exe')
After powershell opens it automatically opens to the python directory. You want to go back to C: with cd... From there use cd users and cd yourusername. It should look like.
PS C:/users/your username>
Set environment with
$env:path="$env:Path;c:/path/to/python/folder"
After this you can do a standard pip install.
python -m pip install <pythonlibrary>

Related

How to fix 'no module named requests'

I have requests module installed but it shows an error when running .py file from cmd prompt.
There are no errors when running the file from vscode.
It seems that requests is installed in your virtual enviroment "my_env" but you must execute your script using the intepreter of your virtual enviroment (It seems that windows keep using the base interpreter even if you activated the virtual env). Try calling it directly, for example
"Your_path_to_venv\Scripts\python.exe" main.py
This issue can be caused by multiple issues.
Wrong pip version is in path (Caused by installing a newer version of python, while an older version was already there.
This can be fixed by simply removing the old python from PATH and restarting the command line.
Try using pip3 instead of pip maybe pip installs libraries in a different dictionary than pip3 does
If that didn't work then use
python -m pip install “your library”
And if you were using python3 just do the same
python3 -m pip install ”your library”
If they did not work too, replace pip in the last two commands with pip3
And if it still does not work first see the path of the python site-packages files by running this python code
import os
import inspect
print(os.path.dirname(inspect.getfile(inspect))+"/site-packages")
a path will be printed, take it and add it as a parameter to your pip command
pip install -t “the printed path”
Ok, I see that you probably have a script called requests.py in your C:\Users\BokaBu>pyproj\my_env\Scripts\activate folder, but your question is how can C:\Users\BokaBu>pyproj\src\main.py find it.
The solution is to add C:\Users\BokaBu>pyproj\my_env\Scripts\activate to PYTHONPATH. The solution to the general version of your question can be found here:
Importing files from different folder - Stackoverflow
But in more detail, you may want to try this to your C:\Users\BokaBu>pyproj\src\main.py:
# In C:\Users\BokaBu>pyproj\src\main.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, 'C:\Users\BokaBu>pyproj\my_env\Scripts\activate')
import requests
Hope it helps~

Not able to "import win32com.shell.shell" in python3.8.3 to execute admin command prompt commands using python3

We were using python 2 in our project and we had created various scripts that work on Windows 10 using pywin32 lib and were using import win32com.shell.shell as shell and then execute the shell commands like shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c ' + commands) where commands is the command which we use to execute as admin prompt.
Our script needs to do some installation which we pass as command and recently due to executive decision we have to move to python3 and when I am trying to import import win32com.shell.shell as shell it is not able to import it.
Can someone please suggest how can we execute shell command as admin in python 3.8.3 on Windows 10?
win32com.shell.shell as shell would be imported exclusively on python2 if you wanted to upgrade you would have to update to a newer version of pywin32. A github repo has released v225 which supports Python 3.8.3 install the files and you should be able to use your code without any import errors
https://github.com/CristiFati/Prebuilt-Binaries/tree/master/PyWin32/v225
if that does not work that alternative solution is using a replica module
pip3 install pypiwin32
import pypiwin32
this module should come with shell capabilities

python27 and python3 are not not recognized as an internal or external command, operable program or batch file [duplicate]

I am using Python 3.5.2 version on Windows 7 and tried using python3 app.py. I am getting this error message:
'python3' is not recognized as an internal or external command,
operable program or batch file.
Is there any specific cause about why the python3 command is not working?
I also verified that the PATH is added to environment variables.
There is no python3.exe file, that is why it fails.
Try:
py
instead.
py is just a launcher for python.exe. If you have more than one python versions installed on your machine (2.x, 3.x) you can specify what version of python to launch by
py -2 or
py -3
You can also try this:
Go to the path where Python is installed in your system. For me it was something like C:\Users\\Local Settings\Application Data\Programs\Python\Python37
In this folder, you'll find a python executable. Just create a duplicate and rename it to python3. Works every time.
Python3.exe is not defined in windows
Specify the path for required version of python when you need to used it by creating virtual environment for your project
Python 3
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
Python2
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
then activate the environment using
.\environment\Scripts\activate.ps1
Yes, I think for Windows users you need to change all the python3 calls to python to solve your original error. This change will run the Python version set in your current environment. If you need to keep this call as it is (aka python3) because you are working in cross-platform or for any other reason, then a work around is to create a soft link. To create it, go to the folder that contains the Python executable and create the link. For example, this worked in my case in Windows 10 using mklink:
cd C:\Python3
mklink python3.exe python.exe
Use a (soft) symbolic link in Linux:
cd /usr/bin/python3
ln -s python.exe python3.exe
In my case I have a git hook on commit, specified by admin. So it was not very convenient for me to change the script (with python3 calls).
And the simplest workaround was just to copy python.exe to python3.exe.
Now I could launch both python and python3.
If python2 is not installed on your computer, you can try with just python instead of python3
For Python 27
virtualenv -p C:\Python27\python.exe django_concurrent_env
For Pyton36
virtualenv -p C:\Python36\python.exe django_concurrent_env
Enter the command to start up the server in that directory:
py -3.7 -m http.server
I had a related issue after installing windows 11, where python3 in cmd would open the windows store. I was able to sort it out between this post and this other one. In short, I reinstalled python and made sure to add it to PATH. Then, in settings, Apps > Apps & Features > App Execution aliases. Here, all I had to do was make sure that every single python .exe (including idle and pip) were turned off EXCEPT FOR the python3.exe alias. Now it works like a charm.
FWIW:
The root of this issue is not with you or with python. Apparently, Microsoft wanted to make installing python easier for young kiddos getting interested in coding, so they automatically add an executable to PATH. For those of us that already have this executable, it can cause these issues.
Found out instead press the play button the top right and it should work in visual studios:
Do not disable according to first answer
Saying python3 in the command will not work by default.
After figuring out the problem with the modules (Solution): https://youtu.be/paRXeLurjE4
Summary:
To import python modules in case of problem to import modules:
Hover over python in search:
Click open in folder
Hover over and right click
click properties
copy everything in path before \python.exe
close those windows
For cmd (administrator):
cd --path that was copied--
then python -m pip install --upgrade pip
cd Scripts
pip install "Name of Package" such as pip install --module (package) --
Im on win10 and have 3.7, 3.8 and 3.10 installed.
For me "python" launches version 3.10 and does not accept commands (like -3.7), "py" launches newest version but does accept commands, and "python3" does nothing.
Uninstalled 3.10 and "python" now does nothing, and "py" launches 3.8.
I am unable to add a comment, but the mlink option presented in this answer above https://stackoverflow.com/a/55229666/8441472 by #Stanislav preserves cross-platform shebangs at the top of scripts (#!/usr/bin/env python3) and launches the right python.
(Even if you install python from python.org, Windows will direct you to the app marketplace nowadays if you type python3 on the command line. If you type python on the same cli it will launch the python.org version repl. It leads to scripts that generate no output, but more likely silently failed completely. I don't know ho common this is but have experienced it on a couple of different devices)
If you have this at the top of your script to ensure you launch python3 and don't feel like editing everything you own, it is not a bad approach at all... lol.

Python modules not importing

New to python and I cannot import modules that I have installed via pip.
For instance, I have installed numpy though cannot import it.
I have a feeling from trying to work this out that it is installing to the wrong directory, or I am calling the wrong version.
$ which python
returns
/usr/bin/python
I am just not sure how to change it so I can access the modules.
First if all, you are installing the packages using pip, which means you install it on python 2 by the default configurations.
The issue you are describing can be caused by several problems:
If you are working with an IDE like pycharm- your project interpeter might by python 3.x. You should change it to python 2 since you used pip and not pip3.
Some newer versions of pycharm are opening virtual environment by default in new projects. This means that if you install packages outside the virtual environment you will not be able to access them. When opening a project, intended of applying the default settings, change the interpreter to your system interpreter, probably your python2.7 in your case.
You are not using an IDE but accecing python from your terminal like so: python3 instead of python.
Hope it helps ;)
From a terminal try:
pip install numpy --user
This install numpy to your home directory. Sometimes this helps compared with installing it without the '--user' flag.
Then:
python
Now you have a python command line. Try:
import numpy
If you don't see any error message, then the install worked. Control-d or typing 'exit()' returns you to your shell.

I installed pydicom through pip but it somehow cannot be found when I try to import the library. Could someone give a suggestion to fix this?

Please see this screenshot:
Isn't the package pydicom already installed?
Credit to Igor:
It looks like that it's working in Sublime Text 2's build using "import dicom". However it is still somehow not working in my Eclipse with PyDev environment.
***I have solved the problem in Eclipse after adding a path in the Python Interpreter configuration by hand (Somehow Auto-Config didn't add this path.).
I did the same series of steps and saw the same issue. After digging around it looks like the package appears as "dicom". Try this instead:
import dicom
Edit: Looking at this question mentioning this library, everyone is importing it this way too.
try:
python -m pip freeze
Is the package there? If not, you have installed it for the wrong python environment. I suggest you install it as follows:
In Bash prompt (terminal):
python -m pip install pydicom
You can change python with python3 or the absolute address of the executable. If you don't know the absolute address of the executable, you can obtain it as follows:
In Bash prompt (terminal):
which python
(or python3). The output would be the absolute path, which you may utilise like so:
/Users/xyz/bin/python -m pip install pydicom
Finally, if you want to find out the path to your executable from within Python, you may do so like this:
from sys import executable
print(executable)
The output will be the absolute path to the current environment's interpreter.

Categories