How can I use module and python on cmd? - python

I am trying to use python and module package on cmd. I could find module directory and start the python program, but the python program opened on cmd did not find the module. I think this problem has occurred because of setting path of module directory is wrong. this is my sys.path on my python program
['', 'C:\Users\skdbs\AppData\Local\Programs\Python\Python37-32\Lib\idlelib', 'C:\Users\skdbs\AppData\Local\Programs\Python\Python37-32\python37.zip', 'C:\Users\skdbs\AppData\Local\Programs\Python\Python37-32\DLLs', 'C:\Users\skdbs\AppData\Local\Programs\Python\Python37-32\lib', 'C:\Users\skdbs\AppData\Local\Programs\Python\Python37-32', 'C:\Users\skdbs\AppData\Local\Programs\Python\Python37-32\lib\site-packages']
and this is what happen in my cmd window
Microsoft Windows [Version 10.0.17134.523]
(c) 2018 Microsoft Corporation. All rights reserved.
C:\Users\skdbs>cd AppData
C:\Users\skdbs\AppData>cd Local
C:\Users\skdbs\AppData\Local>cd Programs
C:\Users\skdbs\AppData\Local\Programs>cd Python
C:\Users\skdbs\AppData\Local\Programs\Python>cd Python37-32
C:\Users\skdbs\AppData\Local\Programs\Python\Python37-32>set PYTHONPATH = C:\Users\skdbs\AppData\Local\Programs\Python\Python37-32\PythonModule
C:\Users\skdbs\AppData\Local\Programs\Python\Python37-32>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
'>>> import game
Traceback (most recent call last):
File "<'stdin>", line 1, in <'module>
ModuleNotFoundError: No module named 'game'
'>>>
(P.S. Thank you for reading my poor english)

Related

Python error running script from command line due to window-build-tools

I installed window-build-tools recently and I found it doesn't work in command prompt.
I manage to find that the path of the interpreter is different.
When I run from VScode(Teamsjoin.py):
import sys
import pyautogui
print(sys.executable)
>C:\Users\1\AppData\Local\Programs\Python\Python39\python.exe
When I type python and run this from command Prompt.
C:\Users\1>python
Python 3.9.1 (tags/v3.9.1, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'C:\\Users\\1\\AppData\\Local\\Programs\\Python\\Python39\\python.exe'
But when I run script from command prompt:
C:\Users\1\Documents\Teamsjoin.py
>C:\Users\1\.windows-build-tools\python27\python.exe
Traceback (most recent call last):
File "C:\Users\1\Documents\Teamsjoin.py", line 3, in <module>
import pyautogui
ImportError: No module named pyautogui
I want to select python 3.9.1 interpreter in command prompt. System environment variables don't include any of the window build-tools. Please help

Python 2.7 Anaconda2 installed in windows

My Python 2 environmental path:
C:\Python27
C:\Python27\Scripts
My Python 3 environmental path:
C:\Python35
C:\Python35\Scripts
I set the environmental path for Anaconda2
C:\Users\User\Anaconda2\Scripts
C:\Users\User\Anaconda2
But when i typed python to enter the shell in cmd (C:\Users\user)
Importing the module of Anaconda like numpy or matplotlib
C:\Users\User>python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named numpy
C:\Users\User>cd Anaconda2
C:\Users\User\Anaconda2>python
Python 2.7.12 |Anaconda 4.1.1 (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import numpy
>>>
So i don't know
1.Why my module can't import while not in Anaconda2
2.It is said that the path of Python2 will overrdie the Python,so how to enter in the Python35 shell?
thanks everybody
Each Python installation has its own libraries. As you will see, you are not running the same Python 2.7 interpreter when you run with Anaconda active as you are without (I assume that's either the system Python or one you installed yourself).
Libraries installed in one interpreter aren't available to others. You should consider learning about conda environments to allow you to manage multiple projects easily.
The command deactivate should terminate the Anaconda environment, and if the Python 3 interpreter is first on your PATH you should then be able to run it. Another way would be to explicitly use the command
C:\Python35\python
which should work even with Anaconda active.
Caution: it's a long time since I used Windows, and I don't have current information on getting multiple Pythons to live happily together on Windows.

Global Python package

so I have this package. In the cmd I go to hp#HP-PC C:\Users\hp\Documents\scripts:
hp#HP-PC C:\Users\hp\Documents\scripts
> python
Python 3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import toolzz.printer as t
>>> t.printz()
5
Everything is working fine but I want to have a directory in which I could add my scripts and be able to open my cmd->python->import my package and do whatever I am going to do and not get this instead:
hp#HP-PC C:\Users\hp
> python
Python 3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import toolzz
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'toolzz'
>>>
PS: keep in mind I have added the directory scripts to the path and I have global bat files which work
Run
python -m site
It'll list 2 important pieces of information:
The Python module search path, sys.path
The location for the USER_SITE directory, and wether or not this exists.
Python looks for modules along those locations. Put your module in a sys.path location (in one that ends in site-packages preferably), or make sure you created the USER_SITE directory and put your code in there.
And you can always extend the path by setting the PYTHONPATH environment variable.

Compile OpenCV for Python wit contrib modules on Windows 10 using Visual Studio 2013

I want to install OpenCV 3 with contrib modules on windows 10 and use it with Python 2.7 (32 bits). I think that the only way to have contrib modules in opencv is compiling it. (not installing with *.exe).
I have compiled OpenCV with contrib modules without problems. I have used Visual Studio 2013, compiling in release mode (for Win32).
After compile OpenCV, I can see the "cv2.pyd" module in $PYTHON_HOME/Libs/site-packages
The problem:
When I try to import cv2 in Python IDLE I have this error:
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: The specified module could not be found.
As you can see, I have a 32 bits Python. Also, the module is "detected".
What's the problem?
I have found the problem.
I had to add the OpenCV *.dll to PATH environment variable. This *.dll are in
$OPENCV_HOME/bin/release

Why would a Python import succeed when invoking *.py file from python but not when invoked by file name alone

I'm in the early stages of integrating some Python to invoke some computations in Matlab and roll up all the figures into some html generation I'll be doing in ll.xist. I had initially installed Python 2.7.5 32bit, but with a 64bit Matlab R2015a installation I could not install the matlab engine for that version of python. I then downloaded a 64bit version of Python 2.7.9 and tried to do a pip into ll-xist, which was apparently not well maintained and failed. I then downloaded Python 3.4.3 64bit, downloaded the ll.xist installer ll-xist-5.13.win-amd64-py3.4.exe and thought I was good to go. I had to do a pip to pull in cssutils, which succeeded.
Now I come to the point where I invoke my python file from the console. If I invoke it preceded with python everything works fine, if I don't the import isn't recognized. What could account for that discrepancy? Looks like some installation snafu between the various versions I've installed, somehow the python version on the path isn't being called appropriately and I'm guessing the 2.7.9 version of python is being invoked somehow because that install never had ll.xist installed, though my 2.7.5 install did.
C:\Temp>python MyScript.py a.txt b.txt
file1: a.txt
file2: b.txt
C:\Temp>MyScript.py a.txt b.txt
Traceback (most recent call last):
File "C:\Temp\MyScript.py", line 20, in <module>
from ll.xist import xsc
ImportError: No module named ll.xist
Here's a sanity check too...
C:\Temp>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
C:\Temp>which python
/cygdrive/c/Python34/python
Check the file association for .py. While the proper installation might be in your path, the file association might still be pointing to a different version.
You can check this by importing the sys module in a test script and call print(sys.version). Then run the test script with python test.py and just test.py to see which versions are printed.
Turns out the governing issue here is what is known as the Python Launcher for Windows.
3.4. Python Launcher for Windows New in version 3.3.
The Python launcher for Windows is a utility which aids in the
location and execution of different Python versions. It allows scripts
(or the command-line) to indicate a preference for a specific Python
version, and will locate and execute that version.
From my console, I can see the "default" version via the py command...
C:\Users\me>py
Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
C:\Users\me>py -3
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Here's the fundamental fix...
The key benefit of this is that a single launcher can support multiple
Python versions at the same time depending on the contents of the
first line.
The first line I had to add in my case to my script was
#! python3
Great commentary linked here for details and motivations behind the Launcher.

Categories