I need to provide my version of Python and packages for a project.
How can I do that?
I tried:
sudo apt-get install python3-venv
virtualenv my-env -p python3
source tutorial-env/bin/activate
This should show installed packages, but it shows:
pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
pkg-resources (0.0.0)
setuptools (39.0.1)
This is also suspicious:
(tutorial-env) linux#LINUXMINT:~$ pip freeze > requirements.txt
(tutorial-env) linux#LINUXMINT:~$ cat requirements.txt
pkg-resources==0.0.0
(tutorial-env) linux#LINUXMINT:~$ python -m pip install -r requirements.txt
Requirement already satisfied: pkg-resources==0.0.0 in ./tutorial-env/lib/python3.6/site-packages (from -r requirements.txt (line 1))
And I cannot find requirements.txt in my directory.
Pass the version of python when creating your virtualenv like this:
virtualenv my-env -p python3
I think you should not use any external module for creating a virtual environment.
You can create a Virtual Environment by using venv attribute of Python in Command Line.
What is the syntax?
The syntax is pretty simple.
C:\>python -m venv path\to\where\you\want\to\create\it
It can be easily done, and you can get a reference from the example below:
C:\>python -m venv "C:\Users\Bhavyadeep\Desktop\Discord Bots\Bot" 1\Bot-1-env
Here the name Bot-1-env is the name of the folder which will be created on execution of the command, and it doesn't have to exist.
What if I am using an IDE (like VS Code), then how will I create a virtual environment?
Creating a Virtual Environment in an IDE is much easier than creating it using CMD. In CMD you need to specify the full path of the directory where the Environment has to be created whereas in an IDE you can create one using its own terminal and also there won't be any need of adding and full path to the directory.
Syntax for IDEs with their Terminals is:
C:\>python -m venv My-Env
This would simply create a Virtual Environment in the folder of the project you are working on in the IDE. If you still want to create it using full path you can do the same as above in the Terminal of IDE.
Example with Images and Code in One Step:
My target directory would be the Desktop for now to explain.
Write and Execute the command in Command Line.
I entered the line in the image and pressed Enter.
Here the name of the Folder would be Example-Venv and it doesn't exist. This command created a folder with that name and created that a Virtual Environment.
This command created a folder, and it can be seen in the picture below.
Now you can use it anywhere you want by simply making this folder as the Interpreter.
How to set interpreter?
The following links would explain:
Pycharm: https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html
VS Code: https://code.visualstudio.com/docs/python/environments#:~:text=To%20select%20a%20specific%20environment,or%20library%20versions%20as%20needed.
Spyder: https://www.spyder-ide.org/blog/release-spyder-330/#:~:text=Just%20set%20the%20path%20under,start%20in%20the%20selected%20environment.
Sublime: https://medium.com/#hariyanto.tan95/set-up-sublime-text-3-to-use-python-3-c845b742c720
I was glad to help! If you still get any problem, please feel free to ask in the comments and I would gladly help you! :)
Thank You! :)
Related
I have created a virtual environment for my Python project and have installed my requirement.txt in it and every time my Python code ex executes it first activate my virtual environment and then run the .py code which is what I want.
I have an issue here - I want to do everything programmatically.
Below are the high level steps i'm looking for :
1)pipreqs requirements.txt using py code
2)check if .txt file has changed, create a flag(1,0)
3)check if virtualenv is created
2.1 - if not created, create and install 1 and run py code
2.2 - if created - check if flag=1 then re-install 1 and run py code
if flag=0 , just run py code
I'm struggling hard to achieve this. Could somebody help.
maybe
pip install -r requirements.txt
is a good starting point (if your paint point is to manually install all the required libraries). It installs all the libraries listed in requirements.txt.
If this is not enough putting this command in a bash / cmd file might be an option.
I have the following small script in a file named fgh.py which I have been attempting to schedule as a webjob
import pandas as pd
df=pd.DataFrame({'a':[1,2,2],'b':[5,6,9]})
df['x']=df.a.sub(df.b)
print(df)
Using #Peter Pan post. I have created a virtual environment, done a pip install pandas. From the virtual environment, the script runs and executes as required.It however does not execute when loaded in Azure Webjobs. I suspect issues arise from the interface between the run,bat file and the Azure python console but have limited understanding of Azure to resolve the issue
In kudus, I have used this post to install python.
Running where python in cmd command in https://myapp.scm.azurewebsites.net/DebugConsole I get;
Additionally from https://arcgistrial.scm.azurewebsites.net/DebugConsole I get the following when I run cmd command python -V
In my run.bat file, I have tried to use either of the directories above without success.
Whether I make my run.bat file D:\home\python364x64\python.exe fgh.py or D:python364x64\python.exe fgh.py I get the following error;
I have gone a head and installed pandas and checked if successful by trying to install numpy
All this has not helped. I have been on this for a couple of days and it has to work somehow. Any help?
(Things are not quite straightforward in old Webjobs to run python task with dependencies. It has been quite some time, the world has moved on to Azure Function :))
However, since you still need to stick to Webjobs, below are the steps I followed which worked. I am using a batch file (.cmd) to run the python script due to the pre-requisites.
By default webjob supports python 2.7 at this moment. So, add python3 from 'extension' in your web app, In this case it was 3.6.4 x64 for me. This will add in path D:\home\python364x64\. How did I know? Kudus console :)
Create a requirements.txt file which contains pandas and numpy (note I had to explicitly add numpy version 1.19.3 due to an issue with latest 1.19.4 in Windows host at the time of this writing). Basically I used your fgh.py which depends on pandas which in turn depends on numpy.
pandas==1.1.4
numpy==1.19.3
Create a run.cmd file having the following content. Note 1st line is not needed. I was just checking python version.
D:\home\python364x64\python --version
D:\home\python364x64\python -m pip install --user --upgrade pip
D:\home\python364x64\python -m pip install --user certifi
D:\home\python364x64\python -m pip install --user virtualenv
D:\home\python364x64\python -m virtualenv .venv
.venv\Scripts\pip install -r requirements.txt
.venv\Scripts\python fgh.py
Zip fgh.py, run.bat and the requirements.txt files into a single zip. Below is the content of my zip.
Upload the zip for the webjob.
Run the job :)
Ignore the error "ModuleNotFoundError: No module named 'certifi'", not needed.
The key to solving the problem is that you need to create your venv environment on azure.
Step 1. Run successfully in local.
Step 2. Compress your webjob file.
Step 3. Upload webjob zip file.
Because the test environment has python1 before, I will create a webjob of python2 later.
Step 4. Log in kudu.
① cd ..
② find Python34, click it.
③ python -m venv D:\home\site\wwwroot\App_Data\jobs\continuous\python2\myenv
④ Find myenv folder.
⑤ active myenv, input .\activate.bat.
D:\home\site\wwwroot\App_Data\jobs\continuous\python2\myenv\Scripts>.\activate.bat
⑥ Back to python2 folder, and input pip install pandas.
⑦ Then input python aa.py.
I use Pycharm to create and manage my virtualenvs in my projects.
The problem is that after adding a library with pycharm, when I type the command (pip3 freeze --user), the library does not appear in the command result.
I have to manually type the pip install command each time so that the library is visible.
What manipulation should I do in PyCharm to solve this problem?
For what you are saying, the first thing that comes to mind is that you should use:
pip freeze
And not
pip3 freeze
Because the command mapped to the pip version when you have virtualenv activated is the first. Note that for installing you seem to use pip, and not pip3
Moreover, the --user option afaik is related to the packages installed in the user folder:
--user Install to the Python user install directory for your platform. Typically
~/.local/, or %APPDATA%\Python on
Windows. (See the Python documentation for site.USER_BASE for full details.)
If your packages are installed in the virtualenv folder, I would tell you to not use that option.
Also please make sure you have your virtualenv activated. In linux you can do so by source path/to/virtualenv/activate
Edit
I understand that the reason you are using pip3 is because you may have different versions of Python in your machine. Let me explain you a bit further how it works, because version management is usually a headache for many programmers and it is common to find problems when doing so.
If you install different versions of Python in your linux machine, and you do that as root, then the installation will proceed for the whole system. Usually Python2 installation folder for Linux machines is /usr/bin/python. However, I am uncertain of which directory is used for Python3 installations. You can check that easily by doing whereis python3. You can serach the path to binary of any command by doing whereis command. Note that this works also for whereis python as far as you don't have virtualenv activated.
Aditionally, the link to the binary of a command (or the set of instructions to be exectued, more broadly) is defined in certain folders in Linux, depending on whether you created the command as root or as a user, and possibly also on the distro. This works differently in Windows, that uses the Registry Edit utility to handle command mappings. When you enable your virtualenv, what you are doing is creating an environment that enables mapping system commands such as python to the Python installation in your virtualenv folder.
When you disable the virtualenv, the command points again to the default installation path. Same happens with pip, so incorrect usage of this tool may result in different packages being installed in different locations, and therefore not appearing available for the right Python version at any given circumstance.
In Linux, environment variables are shell dependent, though you can write them out with echo $variable and set them with variable=value (from bash). The search path is simply called PATH and you can get yours by typing echo $PATH.
Source: https://askubuntu.com/a/262073/426469
I encourage you to check other questions in SE network such as this: https://unix.stackexchange.com/a/42211/96121, to learn more about this.
Addendum
Quick tip: it is common to use the pip freeze command as follows:
pip freeze > requirements.txt
It is a standard that leads to understanding that modules in such file are required for the correct functioning of your application. That lets you easily exclude the virtualenv folder when you install the program in another computer, since you can readily know the requriments for a fresh installation. However, you can use the command as you want.
I am using Python 3.5.2 version on Windows 7 and tried using python3 app.py. I am getting this error message:
'python3' is not recognized as an internal or external command,
operable program or batch file.
Is there any specific cause about why the python3 command is not working?
I also verified that the PATH is added to environment variables.
There is no python3.exe file, that is why it fails.
Try:
py
instead.
py is just a launcher for python.exe. If you have more than one python versions installed on your machine (2.x, 3.x) you can specify what version of python to launch by
py -2 or
py -3
You can also try this:
Go to the path where Python is installed in your system. For me it was something like C:\Users\\Local Settings\Application Data\Programs\Python\Python37
In this folder, you'll find a python executable. Just create a duplicate and rename it to python3. Works every time.
Python3.exe is not defined in windows
Specify the path for required version of python when you need to used it by creating virtual environment for your project
Python 3
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
Python2
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
then activate the environment using
.\environment\Scripts\activate.ps1
Yes, I think for Windows users you need to change all the python3 calls to python to solve your original error. This change will run the Python version set in your current environment. If you need to keep this call as it is (aka python3) because you are working in cross-platform or for any other reason, then a work around is to create a soft link. To create it, go to the folder that contains the Python executable and create the link. For example, this worked in my case in Windows 10 using mklink:
cd C:\Python3
mklink python3.exe python.exe
Use a (soft) symbolic link in Linux:
cd /usr/bin/python3
ln -s python.exe python3.exe
In my case I have a git hook on commit, specified by admin. So it was not very convenient for me to change the script (with python3 calls).
And the simplest workaround was just to copy python.exe to python3.exe.
Now I could launch both python and python3.
If python2 is not installed on your computer, you can try with just python instead of python3
For Python 27
virtualenv -p C:\Python27\python.exe django_concurrent_env
For Pyton36
virtualenv -p C:\Python36\python.exe django_concurrent_env
Enter the command to start up the server in that directory:
py -3.7 -m http.server
I had a related issue after installing windows 11, where python3 in cmd would open the windows store. I was able to sort it out between this post and this other one. In short, I reinstalled python and made sure to add it to PATH. Then, in settings, Apps > Apps & Features > App Execution aliases. Here, all I had to do was make sure that every single python .exe (including idle and pip) were turned off EXCEPT FOR the python3.exe alias. Now it works like a charm.
FWIW:
The root of this issue is not with you or with python. Apparently, Microsoft wanted to make installing python easier for young kiddos getting interested in coding, so they automatically add an executable to PATH. For those of us that already have this executable, it can cause these issues.
Found out instead press the play button the top right and it should work in visual studios:
Do not disable according to first answer
Saying python3 in the command will not work by default.
After figuring out the problem with the modules (Solution): https://youtu.be/paRXeLurjE4
Summary:
To import python modules in case of problem to import modules:
Hover over python in search:
Click open in folder
Hover over and right click
click properties
copy everything in path before \python.exe
close those windows
For cmd (administrator):
cd --path that was copied--
then python -m pip install --upgrade pip
cd Scripts
pip install "Name of Package" such as pip install --module (package) --
Im on win10 and have 3.7, 3.8 and 3.10 installed.
For me "python" launches version 3.10 and does not accept commands (like -3.7), "py" launches newest version but does accept commands, and "python3" does nothing.
Uninstalled 3.10 and "python" now does nothing, and "py" launches 3.8.
I am unable to add a comment, but the mlink option presented in this answer above https://stackoverflow.com/a/55229666/8441472 by #Stanislav preserves cross-platform shebangs at the top of scripts (#!/usr/bin/env python3) and launches the right python.
(Even if you install python from python.org, Windows will direct you to the app marketplace nowadays if you type python3 on the command line. If you type python on the same cli it will launch the python.org version repl. It leads to scripts that generate no output, but more likely silently failed completely. I don't know ho common this is but have experienced it on a couple of different devices)
If you have this at the top of your script to ensure you launch python3 and don't feel like editing everything you own, it is not a bad approach at all... lol.
I just began to use pyenv to manage my python versions, and began to use the pyenv virtualenv plugin to manage my virtualenvs, and so far, I have loved it. One thing I miss however, is that with virtualenv, you could actually place virtual environments in repository directories so that your repository was a completely reproducible environment. Does anyone know of a way to choose the directory of your virtualenv in pyenv?
Short answer: You can’t, as far as I know.
It wouldn’t really work either, right? If you use pyenv virtualenv to install a virtualenv into a repo, and you clone that repo to another machine… how would pyenv on the new machine know to take control of the virtualenv in the repository?
Also, “you probably shouldn’t do that”. Virtualenvs are not 100% decoupled from the underlying Python installation, and aren’t really all that portable. And do you really want to litter your repositories with a bunch of easily replicated junk? The “right” way to go about things is probably to to maintain a requirements.txt for pip — that way you can easily reproduce your development environment wherever you clone your repo.
That all said, there’s nothing stopping you from using plain old virtualenv to create a virtualenv anywhere you like, even if you installed virtualenv into a Python interpreter under pyenv control. That virtualenv itself will of course not be administered by pyenv, but you can still use it as you always did…
Here is a GitHub issue on the project tracker asking about this: https://github.com/pyenv/pyenv/issues/802
The reply by a project collaborator was:
You can just create virtualenv in any location. To use it from pyenv, create symlink to the environment in ~/.pyenv/versions
although I must say I'm not very satisfied with this solution.
There's a workaround
In your project directory, create a .python-version file. Assuming you already installed some virtual environment using pyenv-virtualenv, then append this line to it(you may want to change the version number):
3.7.1/envs/your-project-name#3.7.1
Whenever you enter into the directory, the correct python version will be used, and it works pretty well.
How about:
pyenv install -s 3.8.3 # install preferred version if needed
PYENV_VERSION=3.8.3 python -m venv /tmp/your/prefered/dir ; source /tmp/your/prefered/dir/activate
Note that this method uses venv, not virtualenv, which is slightly different, but a part of a standard library.
Slightly left-field answer, but I ended up writing a hacky equivalent of pyenv that looks for a virtualenv in a directory and its parents because pyenv doesn't really seem to support this as layed out in the answer about.
I called this script enpy, put it on my path and then called e.g. enpy -c import sys; print(sys.executable)
#!/usr/bin/python3
import os
import sys
from pathlib import Path
NAMES = ("venv", "env", ".env", ".venv")
def find_python():
cwd = Path(os.getcwd())
for direc in [cwd] + list(cwd.parents):
for p in NAMES:
venv = direc / p
if venv.exists():
return venv / "bin" / "python"
else:
raise Exception("Could not find python")
python = find_python()
os.execvp(str(python), [str(python)] + sys.argv[1:])
To build on-top of a previous comment, you can add this to your .zshrc:
pyCreate () {
if [ -d .venv ];then
rm -rf .venv; fi &&
if [ -f .python-version ];then
pyVersion="local"
else
pyVersion="global"; fi &&
location=${1:-requirements.txt} &&
if [ -f $location ];then
action=(pip install -r $location)
else
action=(echo "\n No file: $location \n"); fi &&
PYENV_VERSION=`pyenv $pyVersion` python -m venv .venv &&
source .venv/bin/activate &&
pip install --upgrade pip && $action
}
I use the simple recipe of
python -m venv virt to create the local venv
Link the venv to pyenv with ln -s <local virt> ~/.pyenv/versions/<venv name>
Now you can do pyenv local <venv name> and all other normal operations from pyenv with this venv.
For the interested I have made two CLIs
pyenv-here to create a new venv and set local dir in one go
pyenv-link to link a local virt to pyenv
The top comment is wrong; you certainly can.
Once you create your virtual environment with pyenv virtualenv 3.9.15 your-environment-name, you can specify the directories in which this environment will be used automatically with the following command. In your desired directory, run:
pyenv local your-environment-name
The command will create the necessary .python-version file that includes the name of your environment. From now on, every time you enter the folder, the specified environment will be used.
If you want to just use pyenv specific python version and create virtual env from it in wanted directory, you can for example do this:
~/.pyenv/versions/3.10.8/bin/python -m venv my/path/to/venv