Python's Virtualenv - python

I am using Virtualenv to learn Python. The author of the book I am reading wants no system wide access of Python available during learning, so we created a virtual environment via virtualenv. This is not built-in Python 3 virtual environment functionality, it is the pip virtualenv. It's an issue for me because I cannot figure out how to run a script while inside the virtualenv. Virtualenv's documentation reads that activation (or path naming) isn't required when running from within the virtual environment's directory and although I have moved my file both there and within the Scripts directory, I cannot run it while inside the virtualenv environment. Any help? I am using Python 3.6.1. The code I'm trying to run is:
def local():
m=7
print(m)
m=5
print(m)
I realize it's not even training wheel code, but what I'm trying to ultimately do is be able to run code from within the virtual environment to follow as the book suggests. I'm also using a fully updated Windows 10 OS.
What happens when I run the script is this:
(.virtualenv) c:\users\aiii> cd c:\users\aiii\desktop\learning.python\.virtualenv
(.virtualenv) c:\users\aiii\desktop\learning.python\.lpvenv>scopes1.py
'scopes1.py' is not recognized as an internal or external command, operable program or batch file.
(.virtualenv) c:\users\aiii\desktop\learning.python\.lpvenv>python scopes1.py
python: can't open file 'scopes1.py': [Errno 2] No such file or directory.
(.virtualenv) c:\users\aiii\desktop\learning.python\.lpvenv>
I have placed the script both directly in the learning.python folder where the environments are contained c:\users\aiii\desktop\learning.python\.lpvenv and inside the .lpvenv folder in the Scripts folder since that is where other scripts run from within the virtualenv pip are at c:\users\aiii\Desktop\learning.python\.lpvenv\Scripts\

First, install Virtualenv:
sudo apt-get install python-virtualenv
Then Create Virtualenv:
virtualenv venv #venv is name
For activating virtualenv.First, move to folder, In which you want to enable and run this command:
source venv/bin/activate
Once, Your work is done then disable virtualenv:
deactivate

Related

How can I set up Python virtual environment in VScode so that it views locally-installed modules?

I have created a virtual environment via the command line
python3.11 -m venv .
source ./bin/activate
python -m pip install NAME_OF_MODULE
source deactivate
I can see the installed modules when I run pip freeze (prior to deactiving). So far so good.
Then I launch VSCode, open a file and using the command palette, I click Python: Select Interpreter. I then navigate in the bin folder of the virtual environment to the Python installation, which consists of a short-cut / alias pointing to a global Python installation.
When I do this, I cannot import Python modules located in the virtual environment, only those in the global environment. In other words, it appears to be selecting the global environment.
Do I need to set this up within VSCode (Python: Create Environment) ? If so, I can only get so far as the official instructions (https://code.visualstudio.com/docs/python/environments) do not cover installing packages within a virtual environment.
Thanks
I managed to do this in VSCode, by choosing the Create Environment command from the Command Palette. This created a .venv folder where the Python installation is contained. Then when I go to select the Python interpreter via the Command Palette, it automatically finds the local installation. Then, I installed packages by opening the command line WITHIN VSCode (no need to activate environment - it is already automatically activated).
So the trick is to create the environment WITHIN VSCode, not using the system shell.

Setting environment vairable inside requirements.txt

I am trying to install a python package basicsr through requirements.txt file, and I would like to set an environment variable before doing so.
The usual pip installation goes like this :
BASICSR_EXT=True pip install basicsr
and I would like to translate this in the requirements.txt file.
Can anyone help?
I don't think it's possible in requirements.txt.
If you're using virtualenv (you should), try this tool for virtual environment management: https://virtualenvwrapper.readthedocs.io/en/latest/
It includes scripts, such as postactivate: https://virtualenvwrapper.readthedocs.io/en/latest/scripts.html?highlight=postactivate#postactivate, that are run when the virtual environment is activated.
The local $VIRTUAL_ENV/bin/postactivate script is sourced after the new environment is enabled. $VIRTUAL_ENV refers to the new environment at the time the script runs.
Basically put this in the $VIRTUAL_ENV/bin/postactivate file:
BASICSR_EXT=True

Project downloaded from Github setup errors

I apologize ahead of time if these questions are very dumb. I'm pretty new to sourcing python code from github. The link I am attempting to use is a study from link: https://github.com/malllabiisc/ConfGCN. So far, what I have tried is downloading the code as a zip. Then, I followed the instructions from Github and downloaded Ubuntu to run the shell file setup.sh. However, I am running into errors as after running sudo bash setup.sh in Ubuntu, it gives me this error:
Install python dependencies
setup.sh: line 11: pip: command not found
I have checked out the respective files this references. It calls for:
echo "Install python dependencies"
pip install -r requirements.txt
Inside the requirements.txt file it has a variety of python packages I have already installed inside a Venv in Pycharm. It specifically calls for:
numpy==1.16.0
tensorflow==1.12.1
scipy==1.2.0
networkx==2.2
Previous lines in setup.sh run perfectly fine in terms of updating files included in the folder. Another question I have is in general on how to setup a python package. I am currently using Pycharm CE 2020 and I've attempted creating a python package inside of my workspace. I noticed that it auto generates a init.py file. How can I integrate my downloads from GitHub into my Pycharm Project?
There is no reason to run setup.sh as root because it is just supposed to install some packages which do not necessitates Sudo access. You can simply create a virtual environment and run setup.sh. For setting up environment just run:
$ virtualenv -p /usr/bin/python3.6 myenv # Create an environment
$ source myenv/bin/activate # Load the environment
(myenv) $ ./setup.sh
Once the environment is ready, you should be able to run the code. You can make Pycharm use that environment for executing the code.

Install pip libraries for root user

I have a systemd service that regularly reads the first line of a root-owned file, transforms it and then uses png_util:
import png_util
with open('root-owned-file', 'r') as f:
f.read()
...rest of logic...
Now, when the systemd daemon starts, it doesn't have access to the png_util library I installed with pip (pip install png_util) because that only installs it for the installing user. This also happens, when I start the script with sudo:
ModuleNotFoundError: No module named 'png_util'
If I read a file owned by me and execute the script normally as my user, everything works fine.
The systemd service:
[Unit]
Description=PNG
[Service]
ExecStart=/tmp/pngreader
[Install]
WantedBy=multi-user.target
Is the trick simply using pip install --user root and then setting the PYTHONPATH for the root user somehow?
I think you can get what you need with a virtual environment.
You need to create a virtual environment specifically for that script. You will install all the packages you will need for it with the right versions in that environment. As long as you run your script with that virtual environment active everything will be available.- See the venv documenttion here
To create a virtual environment you run python3 -m venv <your_venv_path> with path being where you want to store it, e.g. ~/.venvs/my_project/
To install packages you first have to make it active and then run pip
source <your_venv_path>/bin/activate
pip install png_util
To here you would have your virtual environment ready and your package installed. If you run your script with your virtual environment active the package will be available.
Now, because your script is a daemon this is how you make sure it runs within your virtual environment. Basically the virtual environment creates a copy of Python in and you just add to your script the instruction to use that "copy" of python. You do it by just adding #!<your_venv_path>/bin/python as the first line of your script.
That way when your script runs it does run within that virtual environment where all the packages are installed.
PS: Potentially everything could work by simply running pip as sudo because it will install the package system wide making it available for all users. But that option is highly discouraged for the security risks it creates, see this post with security risks of running sudo pip
Hope this helps!!

Problems setting up a python 3.7 virtual environment

I am trying to create a new python 3.7 virtual environment on my local computer running Windows 8. I have python versions 3.6, 3.7, and 3.8 installed. Their exe's are named python36, python37, and python, respectively. All three are correctly added to PATH because I can enter each interpreter.
Within my new project's directory I tried to create a virtual environment with python37 -m venv env. It produced an error: Error: [WinError 2] The system cannot find the file specified, but it still created the directory. However the Scripts subfolder is empty except for pythonw.exe.
In this question someone suggests doing python37 -m venv env --without-pip. When I tried this, the activation/deactivation scripts were created, but the virtual environment is using python 3.8.
It is my understanding that venv will create the virtual environment with what ever python exe you use to call it, so I don't understand how this can happen. I've verified that python37 points to the correct place with where python37, and can even enter the 3.7 interactive interpreter.
The problem was that I renamed the python exe's. I don't know exactly what goes wrong, but presumably at some point venv tries to find python.exe and is thrown off by the name.
Changing them back to python.exe and differentiating between the versions with their location fixed the problem.
Edit:
Check out Eryk's comments for more details.
First create folder at any drive then go to that folder and install virtualenv package using pip.
pip install virtualenv
Then create your virtual environment.
mkvirtualenv myvirtualenv
Then use below command to activate virtualenv in windows.
myvirtualenv\Scripts\activate
After this you can install related package in current virtual environment.
The Python Standard Library for Creating Virtual Environment

Categories

Resources