1

So I've written a bunch of Python3 scripts that I want to run on a shared work computer. This computer has python2.7 installed on it, and many scheduled 2.7 scripts, and I have to make sure those continue untouched. So I thought a kind of portable or virtual python3 environment I could keep just for my scripts.

If there was a tweak to the things I tried or a new way I didn't consider, that'd be much appreciated. Also sorry for long post, wanted to be specific.


What I've tried:

1) Installing a portable version of python3 (WinPython 3.7).

Then in the .bat file to run my script I put:

SET PYTHONPATH = "C:\Localdata\...\WPy-3702\python-3.7.0.amd64\Lib\site-packages"
"C:\Localdata\...\WPy-3702\python-3.7.0.amd64\python.exe" test.py

Where test.py is simply:

import six
print(six.__file__)

Now this works fine on my own laptop, returning the winpython site-package directory:

> C:\Localdata\...\WPy-3702\python-3.7.0.amd64\Lib\site-packages\six.py

However, with all the above being equal, the shared computer reverts to the installed 2.7 library.

> C:\Program Files (x86)\Python27\lib\site-packages\six.py

This breaks it of course and seems the root of my issue. I've checked and there is a six.py in the winpython site-packages folder. I've tried different versions/installations of winpython with no luck. (Ideally if someone pointed out a simple command here I am missing that could fix this, that would be great.)

FYI if I run python from the "WinPython Command Prompt.exe" that comes bundled, it is indeed version 3.7, just it won't use the local 3.7 site-package libraries...

2) Installing Anaconda3 and not adding to PATH.

The installation completes, but I'm left with only the Anaconda Prompt in the start menu (no jupyter, spyder etc.) and Anaconda Prompt errors out as soon as I try open it:

> 'C:\Users\...\anaconda3\Scripts\activate.bat' is not recognized as an 
internal or external command, operable program or batch file.

I've experience with this issue on other work PCs and the solution is to remove the default python and reinstall Anaconda, but as I said before I can't touch the current Py2 installation without annoying a LOT of people, so another dead-end.

(Although the installation is somehow botched, the anaconda3 files are still installed, so I tried the same test.py+batchfile trick to the anaconda3 site packages but it does the same deal).

3) Creating a Venv:

Ok I admit I'm a bit naive here, if I read this though: Why virtualenv relies on the global python instead of the local one, after being pulled? it looks like I will have the same issues as above.

From my previous post How to run a python script locally in a virtual environment on someone elses windows machine and have it consistently work? the answer to use pyinstaller is useful for a once off, but I've a lot of scripts I now want to run on the same computer, so a 500MB+ package per script would be too cumbersome.

Declan
  • 574
  • 4
  • 15
  • The correct syntax for the `SET` command is: `set varname=myvalue`. Notice there is not any spaces before or after the `=` symbol. I would also not get in the habit of assigning quotes to the value of your variables. You normally just use the quotes when using the variable itself. – Squashman Nov 01 '18 at 01:07
  • @Squashman, I appreciated the syntax correction although it seemed a nitpick tangent. But colour me surprised, it solved my issue completely. If you mind adding it as an answer I'll accept it. And thank-you. – Declan Nov 01 '18 at 01:40

2 Answers2

1

Option 3, virtualenv is what I would go with. It's great that you were able to set the environment variable from @Declan's comment, but I think using a virtualenv is a pretty good idea even if you don't have python version issues. It keeps your global python environments clean, can enable a kind of vendoring, et cetera.

To set the python version of a new virtualenv you use the -p flag.

Unrelated note: Python 2 will be deprecated in 2020.

Charles Landau
  • 4,187
  • 1
  • 8
  • 24
  • Thanks. Would a virtualenv still work if python3 is not installed on the PC? If so would you know of any links or guides for setting up a venv on another computer in windows? I created a virtualenv in PyCharm, copied the /venv/ folder over to the new computer... Then as with the winpython I set the PYTHONPATH to the venv/Lib/site-packages, and run the venv/Scripts/python.exe but I get a "Fatal Python Error". I don't think I'm starting on the right path. (And thanks for the PSA... I'm curious how my company intends to deal with that) – Declan Nov 01 '18 at 02:44
  • 1
    Virtualenv requires activation, and is only relocatable with caveats. See the user guide: https://virtualenv.pypa.io/en/stable/userguide/ . I do think you would need to install both pythons on the computer in question. – Charles Landau Nov 01 '18 at 03:24
1

When using the set command. Any spaces you use before the equals symbol is part of the variable name itself. So if you want to use that variable you then have to reference it as %myvar % with a space at the end.

It is also not a best practice to assign quotes to a variable. You can use them though to protect special characters in the value of the variable and it also helps avoid trailing spaces at the end of the value of the variable.

 SET "PYTHONPATH=C:\Localdata\...\WPy-3702\python-3.7.0.amd64\Lib\site-packages"
Squashman
  • 13,649
  • 5
  • 27
  • 36