Python hangs while executing pip and virtualenv and brings no results - python

I tried to set up a virtual environment for my project by executing virtualenv myenv. The folder seemed to be generated, but the command hung, and I couldn't execute another command. I had to close the console and restart cmd. The folder was generated, as I said, but I couldn't activate the virtual environment by venv\Scripts\activate.
I met the same behaviour while trying to execute pip freeze > requirements.txt. The file was generated, but it was empty, although I used a lot of packages in my project. When I executed just pip freeze, the list of packages was printed, but the command hung again, and I had to close the console again.
I tried both procedures many times, but with no success. I tried that in Windows cmd and Anaconda Prompt (Anaconda version: Anaconda3 2.4.1; Python: 3.5.1).
EDIT: when I tried to do this for the very first time some days ago, I succeeded in activating the virtual environment, but only for one time.

A simple solution to this (which does work) would be to use Powershell as an Administrator, instead of cmd.
Conversely, use cmd as an administrator, though I would recommend using the much-more powerful Powershell for any and all purposes!
Why this works:
A lot of commands need super-user rights (think root/ sudo in linux) in order to be properly executed.
Since there is no such thing as as sudo in Windows yet, you can implement it via admin privileges.
Cheers!

Related

VS Code debugger not using virtual environment in WSL?

I'm currently learning how to make Django projects but the material uses the Linux command shell and the instructors use Macs. I run a Windows machine, so I have to use WSL and Bash.
I'm trying to use the VS Code debugger with a Python virtual environment created in a Bash terminal with WSL. I create my virtual environments by opening up a terminal in VS Code, entering the Bash terminal, then typing python -m venv venvname.
On the bottom right of VS Code there's something like this where I can click on the Python version number to change the interpreter.
If I then click "Enter interpreter path" then "Find" it brings up a Windows Explorer menu where I can direct it to a python executable, but it seems that virtual environments for Linux (created through a Bash terminal) don't have a python.exe. I have a python file with seemingly no extension that I am also unable to open.
I've tried using a venv created with Windows powershell in VSC just for testing purposes. After creating the venv, VSC gives me a notification ask if I want to use the newly created venv (I get no such prompt when creating venvs with bash) and confirming makes the Python version number also indicate that it's using a venv. The files also has an actual python.exe file but it comes with other problems. I can't run the Activate.ps1 script to enter the venv in VSC. I can activate the venv by running command prompt/powershell outside of VSC as administrator, but it seems to have issues installing older versions of packages when I try to install from file with pip install -r requirements.txt.

Waitress installed via pip, but cannot be executed

Overall, pip is working fine on my server. I have just installed the package waitress and the installation seems successful. I checked it with pip freeze:
$ pip freeze | grep waitress
waitress==2.1.0
Waitress also can be imported via python3:
>>> import waitress
>>>
However, waitress-serve cannot be executed:
$ waitress-serve
Command 'waitress-serve' not found, but can be installed with:
apt install python3-waitress
Please ask your administrator.
I am not a root user on this server. Could this be a reason why the package was installed partially, or am I speculating here?
Since I am not authorized to run apt install and since the simple pip install worked in my virtualenv, I would like to be able to get this to work without using the suggested apt install python3-waitress command.
The conclusion is that, while it is installed both inside and outside virtualenv, it is only actually executable inside it.
When installable Python packages give you an actual entry point, generally it will not be on your path.
When you use a virtual environment, activating the environment puts various parts of that environment onto the path temporarily. This is intended to ensure that commands like python or python3 run the environment's Python, but it also allows those entry points to be found on the path.
A system Python installation (here I mean, not just a Python that comes with your operating system, but also one that you install manually after the fact - but not a virtual environment) will generally not have its library folders on the path by default - only enough to make python and pip work. (On Windows, often even these are not added to the path; instead, a program py is placed in the Windows installation folder, and it does the work of looking for Python executables.) Even if you are allowed to install things directly into a system Python (and you should normally not do this if you can avoid it, even if you're allowed to), they won't be findable there.
Of course, you could execute these things just fine by explicitly specifying their paths. However, the normally correct approach is to just ensure that, when you want to run the program, the same virtual environment is activated into which you installed the package.
(On my system, I have one main "sandbox" virtual environment that I use for all my projects - unless I am specifically testing the installation process, or testing how the code works on a different version of Python. Then I use a wrapper script to open a terminal window, navigate to a folder that contains all my projects, and activate the environment.)
if waitress is installed inside a virtual environment, I think you may have accidentally (or not) gotten out of said virtual environment.
If you are running a virtual environment, you can try the following commands, one after the other:
source venv/bin/activate #venv is assumed to be the name of the virtual environment you are using.
pip install waitress
waitress-serve
anytime you need to use waitress you will need to activate the virtual environment yet again:
source venv/bin/activate
waitress-serve
Please take note I am assuming you are running in a Linux environment
If this is not the issue you are facing, then feel free to expaund a bit more on your question.
Edit: Installing with pip and running it worked perfectly on my virtualenv; see the picture below, running on python 3.8.10

Pygame only working when run from editors

When I run a Pygame script through an editor such as VSC or IDLE, the script works perfectly fine. When opened with Python through the file explorer, nothing appears. This only happens when I add import pygame to the script.
For example,
import pygame
i = 0
while i < 9999:
print(i)
would work only on VSC.
I downloaded and used pip to install pygame, and when I convert to a .exe, it still doesn't run.
As the comment says you likely have multiple versions of python installed.
Probably one you downloaded from python's website or through VSC.
Microsoft Store Python is probably located here:
cd C:\Users\<your name>\AppData\Local\Microsoft\WindowsApps
Uninstall that and check your path to python in VSC. Or delete all versions of python, download it anew, and check the PATH box when installing. This will handle the PATH variable for you, and VSC will ask if that's the path you want to use next time you launch it.
Also, you might want to look into using virtual environments. Python3 has this built in and you can create it via the command
python -m venv venv
then type cd (for powershell/cmd) or source(for bash and Terminal) venv/Script/activate to activate the virtual environment. This way you can avoid installing dependencies globally. When inside the virtual environment you will still use pip to install pygame and other dependencies without breaking you program due to updates.
I'm not sure what causes your .exe issues.
More about virtual environments here.

Question about pip using Python from Windows Store

I have python installed through windows store and I can install programs using pip, but when I try to run said programs, they fail to execute in powershell.
How can I make sure that the necessary "scripts" folder is in my path? I never faced these problems when installing from executable.
For example, "pip install ntfy" runs successfully in Powershell.
The command "ntfy send test" fails telling me the term is not part of a cmdlet, function, etc. etc.
The 'ntfy' program is located here /mnt/c/Users/vlouvet/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0/LocalCache/local-packages/Python37/Scripts/ntfy.exe
What is the recommended way of editing my path so that programs installed via pip are available across windows store updates of the Python language?
In advance
I highly recommend you not to use python installed from the Windows Store, because you'll face such errors, and even more nasty ones.
The easy solution
Create a virtual environment on a more accessible folder, for example in C:\Users\<user>\python. To do so, do the following:
Using PowerShell, go to your user folder, using cd(Note that usually PowerShell already starts inside your user folder. This is an important setting to have, and if not, you should change your powershell starting point to this folder for the future.);
Now that you're in your user folder, type in the PowerShell mkdir python; cd python;
Now, to create a virtual environment, type python -m venv venv;
(You can verify that your virtual enviroment has been created by listing the folders, with the command ls);
You have created a virtual environment. Now, you need to activate it. To activate, run the following: ./venv/Scripts/activate;
Now, you have fully created and activated a virtual environment for your current PowerShell session. You can now install any packages / programs using pip.
After that, the only thing that you need to do is to add C:\Users\<user>\python\venv\Scripts to your Path, and you're good to go.
Caveats
By adding this folder to your Path, you may be using an outdated python version in the future, since the Scripts folder inside your virtual environment also adds a python executable that will be enabled in the path.
The recommended solution
As I stated before, I do not recommend to have the Microsoft Store version of python installed on your machine. That said, you're probably using it for the conveniences of having the latest Python version installed as soon as they're released. To alleviate this need while also getting rid of your MS Store Python. I recommend you using Chocolatey to install python (and pretty much any other programs for development).
What is Chocolatey?
Chocolatey is a package manager for Windows, pretty much like apt-get for Ubuntu Linux or HomeBrew for MacOS. By using a package manager, you get rid of the hassle of always having to execute the (mostly annoying) install wizards on windows.
To install Chocolatey:
Go to chocolatey.org/install and follow the install instructions;
(Recommended: Take a look on their documentation later to see what Chocolatey is capable of);
With Chocolatey installed, take a test drive and see if it is working properly by running choco -v in PowerShell;
By having Chocolatey installed, you can now run choco install python -y. Let's break down this command:
choco install -> The package installer of chocolatey
python -> the name of the package you want to install
-y -> This tells the installer to skip install verification by saying "Yes to All" scripts that will be executed in order to install a package.
With python installed from chocolatey, you can also see that Python is already added to your path - This means that any python package or executable installed globally will be now available on your machine!
Hope I could help you!
The above answer is good but I managed to get it to work by doing the following.
Find your installation under C:\Users\"your user"\AppData\Local\Packages it will be named something like PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0
Open your windows settings in the start menu
In search type Environment variables. Edit environment variables for your account should pop up. Click it
In the top box find Path, click it
On the right Click new and enter C:\Users\"your user"\AppData\Local\Packages\"python install directory name from 1. here"\LocalCache\local-packages\Python37\Scripts inside the little box under the last item in the list
open a new cmd prompt and type the script you wanted it should work.
On Windows you can find the user base binary directory by running
python -m site --user-site
and replacing site-packages with Scripts.
For example, this could return
C:\Users\Username\AppData\Roaming\Python36\site-packages
so you would need to set your PATH to include
C:\Users\Username\AppData\Roaming\Python36\Scripts
You can set your user PATH permanently in the Control Panel. You may need to log out for the PATH changes to take effect.

"pip install somepackage" freezes powershell

I've been using python in both my office pc and my personal laptop and on both machines typing in powershell:
pip install somepackage
works normally.
Strangely enough, when I attempt to do it on my personal desktop, powershell remains on halt infinitely.
I found a solution for this by doing:
python -m pip install somepackage
The above is fine, and I can live with typing an extra 8 characters whenever I want to install a new package but I'm curious to understand what is happening.
Thanks :)
PS: I have added C:/Python27/Scripts to Path in the system variables already and it didn't solve the issue.
pip.exe is located in C:\Python27\Scripts. You need to add that folder to your PATH environment variable. If you added it to the system environment you need to start a new PowerShell console to make the change become effective.
If you want the path to become available in the current console you need to add it to the PATH variable in the current console as well (the console gets a copy of the environment when it starts, it doesn't pick up changes to the system or user environment at runtime):
$env:Path += ';C:\Python27\Scripts'
pip install somepackage
However, C:\Python27\Scripts not being included in the PATH should only result in an error message that PowerShell doesn't recognize the command. It shouldn't freeze the console. Perhaps there's some other cmdlet/command/function/alias named pip that gets executed instead. Try running Get-Command pip to verify that.

Categories

Resources