working with virtualenv without Python in Windows path - python

I have to do some work with a company with security restrictions on their computers.
I need to set Python2.7 virtualenv on their Windows10 machine but can't add python to Windows path. I installed Python through the Windows Software Centre. The interpreter is in usual C:\Python27\python.exe but it is not added to Windows path. When I run python in CMD it is not recognizable although C:\Python27\python opens the interpreter.
The problem is that to add it to Windows path I need admin privileges. It is simply not possible. I know the obvious answer is to contact admin but again it is not an option.
So the problem is, having this setup I need to install virtualenv, inside create all my environment and work on it.
I can't find the way to do it without Python in the path.

I usually install several system-wide pythons versions on my windows boxes with the only purpose of creating virtualenvs and that's more than ok. In fact, i strongly recommend to avoid polluting PATH with system-wide python paths at all. Using just virtualenvs is really a good way to go.
That said, if you're creating virtualenvs manually (ie: not using any IDE/plugin to manage this process for you) you can just do it on the command prompt like this:
> cd c:\python27\Scripts
> pip install virtualenv
and then just using c:\python27\Scripts\virtualenv.exe to create your new virtualenvs, for instance, something like this would do it:
> c:\python27\Scripts\virtualenv --python=c:\python27\python.exe foo_venv
That said, if it's more convenient for you to have virtualenv executable reachable from the console (ie: where virtualenv finds its path) you can just add it temporarily to your console session with something like:
SET PATH=%PATH%;C:\Python27\Scripts
Or even better, just create a shortcut of one bat file which spawns your favourite console (cmd prompt, conemu, ...) setting PATH on that particular session, that way you don't need to persist the variable PATH with admin privileges.

You could run: SET PATH=%PATH%;C:\Python27\ from a command prompt and it will add the python to path temporary (i.e will be gone when the command prompt is closed)

Related

python curses module for windows can't install

i try to install curses module on windows and it ends with this error
picture:https://i.stack.imgur.com/fbKCJ.png
You can't install windows curses with 3.10 yet. It is supported on 3.9. I don't know when it will be supported for 3.10, so your best option for now is just to install 3.9.
You can make a virtual environment with python 3.9 for any projects that need to use curses. A virtual environment makes a copy of your python interpreter and installs it into a directory of your choice. You use this by "activating" the virtual environment, and as long as you're inside that environment, anything you install will be contained by this copied installation.
This allows you to run different versions of python, and it also allows you to install packages that you don't want cluttering up your main installation. It's a good idea to use this for all projects that are going to need packages outside of the standard library (anything that you pip install).
To make a virtual environment with your default interpreter, type:
python -m venv <envname> where <envname> is whatever you want the environments directory to be called. This is usually env.
So python -m venv env would install a fresh copy of your python 3.10 to a folder called env inside your current directory.
You activate this by typing .\<envname>\scripts\activate
You'll then get a prompt that has (<envname>) in front of it, which will let you know you're in that environment.
You can leave that environment by typing deactivate.
In order to use a different version, you have to run venv with the interpreter you want to use on the project. So if you wanted to use python 3.9, it would be something like
"C:\Program Files\Python39\python.exe" -m venv env depending on where you installed python 3.9. The directory I used is usually the default directory when installing for all users.
To more easily work with other versions on windows, I make batch files for each one and put them in a utils folder that's on my system path. (I'll explain how to add a folder to the system path at the bottom if you don't know.)
So make a file called python39.bat, and into that, put "C:\Program Files\Python39\python.exe" %*. (Or wherever the installation is. %* just expands other arguments so you can use it exactly like you would the other executable.
That way you can create a new python 3.9 virtual environment with python39 -m venv env (along with any other arguments you want) instead of typing out the full path.
You can also use --prompt to change the name displayed by your virtual environment instead of changing the name of the folder. This is useful for making it shorter or just keeping things straight when you're using a bunch of environments for different projects. (Using the same folder name allows you have something that doesn't change into your standard ignore files.)
So anyway, here's an example of the full process after you install python 3.9.
Go to your project directory or wherever you'd like to install the environment.
type "C:\Program Files\Python39\python.exe" -m venv env (optional) --prompt somealternatenametodisplay (or python39 -m venv env if you made a .bat file).
type .\env\scripts\activate
You should now have (env) or the alternate name at the beginning of your prompt
type pip install windows-curses
And everything should work now. Just remember to activate your environment whenever you want to use this.
(To put a folder on the path)
Let's make a new folder called myutils as an example at C:\myutils
and put python39.bat in that folder.
Right click My Computer
Select properties
On the right side under Related settings click on Advanced system settings.
At the bottom of the Advanced tab, click on Environment Variables
(You can also get to Environment Variables much faster by opening the start menu and starting to type environment, which should give you Edit the system environment variables).
Under System variables, select Path, and then click Edit...
Type C:\myutils, hit Enter, and press OK.
Now, open a new terminal, and you'll be able to access any programs you put in that folder.
In your path variable in Environment Variables, you can also change the default python interpreter. The default will be whichever one is at the top (which will probably be 3.9 now that you've just installed it).
To change it back to 3.10, select C:\Program Files\Python310\Scripts\ and click Move Up until it's above the Python39 entries, then do the same with C:\Program Files\Python310\.
GO to this URL and find your python version and download wheel file from it
As from my image it is python 3.7 with 64 bit so i will download this file
windows_curses-2.2.0-cp37-cp37m-win_amd64.whl and give whole path where it is downloaded
and give full path for installation like
pip install filepath\windows_curses-2.2.0-cp37-cp37m-win_amd64.whl in cmd or powershell

Setting PYTHONPATH on Windows under Anaconda without elevated privileges

I have a Windows 7 machine with a 32-bit install of anaconda, installed for the local user. The install works as expected: it allows me to run python and pip from the command line, switch environments, etc.
I also have a library of code that I wrote sitting in My Documents. I would like to add an entry like C:\Users\username\Documents\MyLib to my PYTHONPATH. This would normally not be an issue, but I do not have the elevated privileges that this (work-issued) computer requires to modify environment variables through the Windows UI.
I am looking for a way to set PYTHONPATH. I only need it to work within the anaconda environment (i.e., I just want to be able to import my library using that particular interpreter). Solutions using sys.path.append within my scripts are not acceptable. I am, however, OK with doing sys.path.append in some script that will be executed automatically whenever I run python myscript.py.
I suspect that there is a way to get around the privilege restrictions imposed through the Windows UI though, because running conda info -a shows that the Anaconda installer was able to insert items into the PATH, specifically, C:\Users\username\AppData\Local\Continuum\Anaconda3\Library\bin;....
Since you said, you mostly use Spyder, the following might be useful:
Spyder offers the option to manage PYTHONPATH through an integrated manager.
In the Control Panel window for modifying user accounts, there should be an option to change the environment variables just for the current user. It's broken on some versions of Windows, but if it works it's the simplest option.
If that doesn't work, the next simplest option is to use setx.exe in a command prompt. It defaults to the current user. For example:
setx.exe PYTHONPATH "C:\Users\username\Documents\MyLib"
You can also use reg.exe to set the variable manually in the registry. But unlike the above options, this doesn't broadcast a WM_SETTINGCHANGE message to top-level windows. When Explorer sees that message it reloads its environment from the registry. Without it, you'll have to log off and back on again to see the updated environment variable. With that said, here's an example command using reg.exe:
reg.exe add HKCU\Environment /f /v PYTHONPATH /d "C:\Users\username\Documents\MyLib"
I'm not a fan of permanently setting PYTHONPATH because the same variable gets used by every version of Python. I'd rather create a shortcut to a batch script that configures the environment the way I need it for a specific task. For example:
#echo off
set PYTHONPATH=C:\Users\username\Documents\MyLib
C:\Users\username\Documents\MyEnv\Scripts\activate.bat
Create a shortcut to this batch script. Then right-click the shortcut and select "Properties". Modify the target to run cmd.exe /k "path\to\the\script.bat".

Python, virtualenv: telling IDE to run the debug session in virtualenv

My project's running fine in virtualenv. Unfortunately, I can't yet run it via my IDE (Eric) because of the import troubles. It stands to reason, as I never told the IDE anything about virtualenv.
I know the drill ($ source project/bin/activate etc.), but lack general understanding. What constitutes "running inside virtualenv"? What IDE options might be relevant?
I think the only required setting to run or debug code is path to python interpreter.
Relevant IDE options could be SDK or Interpreter settings.
Note that you should run not default python (eg. /usr/bin/python) but python binary in your virtual environment (eg /path/to/virtualenv/bin/python)
Also there are some environment variables set by activate, but i think they aren't needed when you point to virtualenv python binary directly.
So, again, what activate does is only environment variables setup: at least, it modifies system $PATH in a way that python and pip commands points to executable files under path/to/virtaulenv/bin directiry.
As far as I know it is possible to run your script/project using your virtualenv simply by calling /path/to/your/venv/python your_script.py. To install new packages in your venv for example, you would run /path/to/your/venv/pip install some_package.
I guess the main advantage of "runnnig inside virtualenv" would be not being concerned about having to inform the location of python packages/executable everytime you want to run some script. But I lack general understanding too.
I usually install a virtualenv with the --no-site-packages option in order to have a "clean" installation of python.
--- EDIT ---
The second answer of this discussion has a nice explanation.
Simply look at the project/bin/activate, everything you need is there to set up the appropriate search.
Usually the most important path is the PYTHONPATH which should point to the site-packages.

python not starting properly

I have installed python and django in my system that uses win vista. Now when I go to command prompt and type python or django-admin.py both are not working. Every time I need to set the path to the python folder manually. But i have seen these commands running even without setting path. So how do i make it to run properly?
You probably need to add Python to you dos path. Here's a video that may help you out:
http://showmedo.com/videotutorials/video?name=960000&fromSeriesID=96
you can't run a command that isn't in your path. it should be set globally when you installed python.
type 'set' at a dos prompt and look at the PATH variable. c:\python25 (or whever you installed python) has to be in that variable ie PATH=c:\windows;c:\python25;... etc
if it isn't in PATH then you or the installer missed the part where you needed to set it. It can be done from the 'Environment Variables' button in the 'System' control panel.
Either use the system control panel to set the PATH environment variable that applies permanently or
Reinstall Python as a system administrator so that the installer can set the registry and environment variables for you.
If you install the "just for me" option, then you have to set the PATH variable in the control panel.
In your path, I think you need to have both the location of the Python install and the Python\Scripts folder. For example, on XP, I have C:\Python25;C:\Python25\Scripts. Can you verify that you have both?

Can't find my PYTHONPATH

I'm trying to change my PYTHONPATH. I've tried to change it in "My Computer" etc, but it doesn't exist there. I searched in the registry in some places, and even ran a whole search for the word 'PYTHONPATH', but to no avail.
However, it Python I can easily see it exists. So where is it?
At runtime, you can change it with:
import sys
sys.path.append('...')
In My Computer, right-click Properties (or press Win-Break), System tab, Environment Variables, System. You can add it if it's not already there.
Finally, in the CMD prompt:
set PYTHONPATH C:\Python25\Lib;C:\MyPythonLib
Or in bash:
PYTHONPATH=/usr/share/python/lib:/home/me/python
export PYTHONPATH
Or, more succinctly:
export PYTHONPATH=/home/me/python
Python does some stuff up front when it is started, probably also setting that path in windows. Just set it and see, if it is changed in sys.path.
Setting environment variables in the Python docs say:
My Computer ‣ Properties ‣ Advanced ‣ Environment Variables
You can add it under "My Computer" if it doesn't exist. PYTHONPATH just adds to the default sys.path.
On unix/linux/osx you can:
$ export PYTHONPATH=/to/my/python/libs
You can also use .pth files to point to libraries:
http://docs.python.org/library/site.html#module-site
And of course:
import sys
sys.path.append('/path/to/libs/')
Also, check out virtualenv for managing libraries for multiple projects.
Here's how I solved it.
First, get the current path. There's a lot more there than I expected.
import sys
print ';'.join(sys.path)
Copy that result to the clipboard. Go to My Computer and create the new environment variable PYTHONPATH, and for the value paste from the clipboard. Modify as necessary.
MacOS 10.5.8, Python 2.6, Eclipse+Pydev 1.5.7
Python installation's site-package is, for example:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
create symlinks YOUR LIBRARY inside into site-package, for example:
cd /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
ln -s /path/to/YOUR/LIBRARY/ YOUR_LIBRARY_NAME
Now You can use in commandline: import YOUR_LIBRARY_NAME
run Eclipse with Pydev, go to Preferences->Pydev->Interpreter Python
remove Your Python interpreter record, if exists;
click New and add Python 2.6 interpreter path, for example:
/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6
notice, that Eclipse Pydev display Python System Library, accept that
in Library section click New Folder and write path to YOUR LIBRARY, for example:
/path/to/YOUR/LIBRARY/
click Apply - it is essential, because Eclipse Pydev built now his own "library map", when this operation finish - click [OK]
close Eclipse
run Eclipse again - now You should use in Pydev: import YOUR_LIBRARY_NAME
And, as with all good things in life, you can find it in the documentation:
http://docs.python.org/install/index.html#modifying-python-s-search-path
What's it set to? Have you tried creating a PYTHONPATH environment variable?
You need modify your environment variables. How to do this depends on which version of Windows you have.
If the PYTHONPATH variable doesn't exist, you have to create it. It might not exist if you haven't already created it.
I had same problem and oliver-zehentleitner's answer in github solved my problem.
He said: Maybe You install package with pip for python2 and run with python3, just try to install with pip3 or python3 -m pip install python-binance and then run your script again.
I hope this can solve yours too.

Categories