I'm completely new to this. I have set up Python3 and Atom and installed Hydrogen for Atom so I can run each line of my code and see the output.
I have set up a virtual environment and added packages to it.
My problem is that inside my Atom .py file, when I say import numpy as np for example, it tells me that the module is not found. So I think it is looking in some default place and not inside my virtual environment. Which makes sense as I don't know how to tell it to look inside the virtual environment.
I know that inside terminal I can load the virtual environment and then call the .py file from there and it will look in the right place. However that is not what I want to do. I want to be able to tell it to look in the virtual environment in the top line of the code and execute using Hydrogen, and then load the packages I want using Hydrogen, and then carry on with each line of code after that using Hydrogen.
Can someone tell me how to tell python to look in a specific virtual environment for the duration of the .py file that is being developed/executed?
For our purpose here virtual environments are just changing the search path of your interpreter.
Therefore if we want to search in a given virtual environment we can just add the path of this environment to our search path, which you can do in python by using
import sys
sys.path.append('/path/to/virtualenv')
The path to your virtual environment depends on how you configured it, but usually they are stored in a subfolder of your home directory called .virtualenvs, so this would probably look like
import sys
sys.path.append('/home/username/.virtualenvs/EnvName/')
# rest of code
Also note that this does not change your system path or pythonpath environment variables, and therefore only lasts for the duration of this python interpreter instance.
Related
What exactly does setting up and activating a virtual environment do? From what I gather it will change the module search path environment variable for the directory it is installed in? Thus, any subdirectory modules which attempt to import a module will be forced to look in the local sites package folder rather than the global one (as long as the venv is active).
I see that if I activate the venv and run pip list I no longer get the global package list but the local one. However, I can't seem to find a local sites-packages directory...
"what is the mechanism"/"what exactly does ... activating" -- i.e. what does the code do?
You can find an explanation in official docs:
https://docs.python.org/3.11/library/venv.html#how-venvs-work
And you can dive into the sourcecode of <venv>/bin/activate to see what it does. It's not a complex script, and I suggest trying to read through it and asking separate questions about any specific parts you don't understand.
I am using python venv to create virtual environments. But, since I am working with several projects with different virtual environments, I don't want to manualy set environment variables every time I switch to a different project.
Is there a way to set venv environment variables automatically when activating the venv?
What is the best practice for this problem?
A good practice is to use dotenv. You can load your environment by placing your environment variables into a file named .env, and whenever you would like to load an environment, just use the lines:
from dotenv import load_dotenv
load_dotenv()
This has the nicety that it only exists within the scope of you running a single script, since it essentially works like calling os.environ['variable'] = 'value' a number of times.
Activating a virtual environment is nothing more than sourcing a shell script. You can edit that script to set whatever variables you like. You will probably also want to edit the definition of deactivate to clear or roll back whatever changes you made to the environment.
you need to write a bash scirpt (in case you are using bash shell), where you specified a particular command which will activate the project python environment and add the project specific envrionment variable in the system environment. and remove the environment variable when you exit the project python environment.
but i don't this is good/correct way to do things. #mz solution will be correct, where you define a .env file and define env variable in it. and use load_env to read the env variable when project runs
This concept is based on Two Scoops of Django. I have implemented it using venv.
Open the Windows PowerShell Script in your virtual environment generated by venv.
The script is located at venv/Scripts/Activate.ps1
At the bottom of the file, you will see this line of code:
$env:VIRTUAL_ENV = $VenvDir
Below that code, enter your environment variable as follow:
$env:VARIABLE_NAME = 'variable_value'
Same concept goes if you are using the Command Prompt to activate the environment, you will need to place environment variables in venv/Scripts/activate.bat
I am using windows 7. I have multiple python virtual environments. Say I added venv_1 to system PATH. In the command line, say I activate another venv_2, now the prompt line shows
(venv_2) C:\>
But if I type python here, it still runs the python in venv_1.
Is this the intended behavior?
This is not the intended behaviour, but it probably means you either made a mistake in setting up the virtual environment, or in activating it.
To be sure what version is being run, try running:
where python
Whatever the top item in the resulting list is, will be the copy of Python Windows would start. If you're right and it does actually point to venv_1, then there must be something wrong with the setup in venv_2.
By running set, you should be able to see a list of all environment variables. Check for:
PATH=<long list of directory names, it should have the venv_2\Scripts at the start>
And:
_OLD_VIRTUAL_PATH=<the same list, without that entry>
It's this simple change to the path that causes Windows to find the Python in your virtual environment first, before the one in your other virtual environment, that you added to the global path.
Note that adding the Scripts folder of the one virtual environment probably isn't a good idea, since you'd only want to use that when the corresponding virtual environment is activated and all the environment variables set accordingly.
I tried to open a python script with VS code. The library that requires with the script is in a virtualenv.
I am not sure how to integrate this with VS Code. I could not find my virtualenv in the intepreter.
For your information, the virtualenv I created is called "vc-web" and the scripts is in the folder called "web_scraper" inside the "vc-web" folder.
If your project is in folder e.g. proj, and you have a virtual environment at proj/vs-web and it's the only virtual environment in that directory, then it should be listed by the Python: Select Interpreter command where you can select it.
All you need to do is just specify the "python.pythonPath" as follows. The values for "python.autoComplete.extraPaths" will be determine during runtime, however you are still free to specify custom paths in there.
Please remember to re-start VS Code once the necessary changes have been made.
{
"editor.rulers": [80,100],
"python.pythonPath":"~/path/to/venvs/proj/bin/python"
}
I'm new to virtualenv and not sure how to set up paths. My paths have been set to something like this:
PYTHONPATH=C:\Python27\
PYTHONSTARTUP=C:\Python27\Scripts\startup.py
PATH=%PYTHONPATH%;...;%PYTHONPATH%\Scripts
Should I remove those paths for virtualenv's activate script to work correctly? If I can keep my paths then how do I call scripts for an env when it has been activated? Do I call scripts by explicitly running them with python.exe instead of simply typing the script name alone?
python myscript.py
Not sure how to handle the paths and I would appreciate a little guidance.
First, you have your paths wrong. PYTHONPATH tells Python in what folders to look for Python modules and normally you don't put Python's installation folder in it. For keeping installation folder of Python there's different environment variable called PYTHONHOME. So instead of PYTHONPATH=C:\Python27\ you should have PYTHONHOME=C:\Python27\. You should change PATH variable to use PYTHONHOME accordingly.
As to how to set environment variables when working with virtualenv; you don't need to do anything because virtualenv stores original values when it's activated, modifies environment variables it needs to modify and then restores original values when it's deactivated.
You can take a look at Using Python on Windows
Think you are fine just get on with virtual-env, (follow docs) but remember you must use cmd shell (NO POINT AND CLICKING!!) Took me a while before I realized that...
Once you have activated And installed what you want to in the virtual env,, you invoke scripts by "python scriptname"