I have installed virtualenv 1.10 on a Windows 7 machine with Python 2.7 on it.
I have created a virtual env called TESTENV. It is located in C:\
If I run C:\TESTENV\Scripts\activate and then type python followed by:
import sys
print sys.prefix
it outputs C:\TESTENV, as exprected.
But if I run D:\virtualenv_test.py (which is a script containing the two lines of code I used above) it outputs C:\Python27.
I tried adding a shebang line to the script but that doesn't work on Windows.
Why is the script not running in the context of the virtual environment?
virtualenv doesn't change the default application that starts a file.
When you installed Python, the installation associated .py and .pyw files with appropriate C:\Python27\python.exe and C:\Python27\pythonw.exe applications. So when you run (in your cmd) only D:\virtualenv_test.py, cmd looks into the registry and finds the application that is associated with .py files and then runs the file with that application. In this case that is the previously installed C:\Python27\python.exe.
virtualenv activate only adds that particular virtual environments python interpreter at the start of the PATH environment variable, so when you type python in cmd you will get the appropriate python.exe executed.
So the solution to your problem is to run your script as:
python D:\virtualenv_test.py
After you activated the environment.
Related
Maybe a simple question, but I cannot figure it out. I work within Visual Studio Code on a MacBook. Within a directory I create a virtual environment and have activated that with source .venv/bin/activate scripts and everything works fine.
In the terminal I see (.venv) (base) gaston#MacBook-Pro WebScraping %
When I start visual code another time and open this same directory I do not see the (.venv) so I again give manually the command to activate.
My question is there a way to activate the virtual environment automatically when opening the directory?
VS Code Default Python Interpreter Path
You can do this in a number of ways. If your goal is to have the virtual environment selected immediately at the launch of VS Code, you will have to target that virtual environment as the Python Interpreter in VSC.
https://code.visualstudio.com/docs/python/environments
System Preferences->Settings->Python: Default Interpreter Path
You'll want to point it to your virtualenv Python installation, likely something like:
envFolder/bin/python3
VS Code Launch Configuration Python Interpreter
You can also set a specific launch configuration so that VSCode will still use your system Python instance by default; however, specific launch configurations will have the virtual environment specified.
https://code.visualstudio.com/docs/python/debugging
I did something within settings and now it works.
When I open VSC now it opens the directory and the right environment, just the way I wanted it.
Took me some while to figure what finally did the trick:
I opened settings Command + , typed env in the search box. Scrolled through the results until I saw:
Python > Terminal: Activate Env In Current Terminal
and checked the checkbox. That solved my problem.
Using pycharm, I finished my first python program.
I am attempting to run a bat file with Windows Scheduler.
The python script has lots of dependencies. They are available in the virtual environment I am using.
I tried activating my virtual environment using the activate.bat file in the directory of the venv.
The code of the scheduled bat file looked like this:
C:\Users\PavelWorkXPS\Anaconda3\envs\TestEnv\Lib\venv\scripts\nt\activate.bat "D:\Dropbox\Toolkit\Python\Unusual_Options_Activity-ToS\main.py" pause
When running this, Pycharm would launch because it is my default .py extension handler.
I modified the bat file to use the interpreter python.exe from within the virtual environment folder, hoping it would have access to the dependencies.
C:\Users\PavelWorkXPS\Anaconda3\envs\TestEnv\python.exe "D:\Dropbox\Toolkit\Python\Unusual_Options_Activity-ToS\main.py" pause
Output would tell me it still can't see the dependencies.
I used this earlier stackoverflow post for help, but it only explained the reason
why pycharm was opening by itself.
Conda was the reason for the problem. When I switched to doing it with a plain virtual environment, it stored all the downloaded repos in a folder inside of the folder with the script. Then I setup the bat file and all worked fine.
Python 3.8 installed on Windows 10 in C:\Python directory. C:\Python and C:\Python\Scripts added to both system and user environmental variables (path). (See attached image.)
But, Python won't run from a command line. When I type python, the Microsoft Store pops up asking to install Python 3.7.
I have to be in the C:\Python directory and I have to type python.exe for it to run.
Any idea what's going on?
Looks like your launched that command prompt before editing the PATH environment variable, as the Python install directory isn't in the path output. The new environment settings are only applied to fresh prompts, not existing ones.
But really, don't bother with that anyway. Just use the py.exe Windows launcher which is automatically placed in your Windows directory (e.g. C:\Windows) which is already in your PATH. To launch with the latest installed Python 3, just run:
py -3
for the latest Python 2:
py -2
If you had the installer associate the launcher with the .py extension, you can put shebang lines in your scripts and they'll "Just Work". For GUI apps, pyw.exe and the .pyw extension serve the same purpose.
I just installed Anaconda on C:\Anaconda3 on the same computer where the old Python was installed on C:\Python34.
There are tools that manage different version of Python or different versions of Anaconda, but I can't find anything that manages different versions of both Python and Anaconda on the same machine at the same time.
I was able to get both the old app to run with Python 3.4 and the new with Anaconda 3.5 using the 4 scripts below. Double clicking on each of them in sequence the two apps run without problems.
But I wasn't able to make one script to run both the apps at once, not even two scripts, one for the first app and one for the second. Any idea how I would solve this problem?
Python1.cmd
setx PATH "[...]C:\Python34[...]"
setx PYTHONPATH "C:\Python34"
setx PYTHONHOME "C:\Python34"
Python2.cmd
cd \OldFolder
C:\Python34\python app.py
Anaconda1.cmd
setx PATH "[...]C:\Anaconda3[...]"
setx PYTHONPATH "C:\Anaconda3"
setx PYTHONHOME "C:\Anaconda3"
Anaconda2.cmd
cd \NewFolder
C:\Python34\python app.py
Using setx works, but only if the script that executes the setx and the one that executes python are not the same (as in the 4 scripts above). Using setx on the same script is ignored by Python or Anaconda.
Using set on the same script where python is executed causes python to crash immediately. Here are the script and the error:
PythonSet.cmd
set PATH="[...]C:\Python34[...]"
set PYTHONPATH="C:\Python34"
set PYTHONHOME="C:\Python34"
cd \OldFolder
C:\Python34\python app.py
Error
Fatal Python error: Py_Initialize: unable to load the file system codec
ImportError: No module named 'encodings'
You are using SETX, which does not change the environment variable. It saves the enviroment variable value in registry, to be used in future processes.
In order to change the value in current process, use SET:
set PATH="[...]C:\Anaconda3[...]"
set PYTHONPATH="C:\Anaconda3"
set PYTHONHOME="C:\Anaconda3"
I have virtualenv installed on windows.
In cmd, i run python and look at sys.path and see the virtualenv path included.
but when i run manage.py (for django), I don't see the virtualenv path,
so virtualenv is not working with django server.
Why?
the solution is to explicitly invoke python by using: python file.py
as described in http://www.velocityreviews.com/forums/t727997-problems-running-virtualenv-under-windows.html
for some reason, the python registered with .py in windows does not invoke virtualenv.
Virtualenv modifies the PATH to include a Python with the correct setup. It's a completely separate program from the system Python.
The PATH is used to look up programs by name: the first program of a given name that's in the PATH gets executed.
When you “run a file”, Windows uses the extension of the file to look up a program to run. It doesn't look it up by the name of the program, and so doesn't check the PATH.
The solution is to explicitly invoke Python from the command line (python manage.py) while a virtualenv is active. This way, Windows will search PATH for what you meant by “python”, and find the correct one.
have you done:
> source ../path-to/bin/activate
?