How to create virtualenv with specific downloaded python version - python

I know there are some similar questions,but it is really hard for me to finish it.
I'm trying to create a virtualenv with python 3.7.7 in windows.
I have a downloaded python 3.7.7
C:\Users\willi\AppData\Local\Programs\Python\python-3.7.8-embed-amd64
Since I can built a virtualenv using:
python3 -m venv myenv
So I tried to modify it ,so that it can match specific python version:
python3 -m C:\Users\willi\AppData\Local\Programs\Python\python-3.7.8-embed-amd64\python.exe myenv
But it failed:
ModuleNotFoundError: No module named 'C:\\Users\\willi\\AppData\\Local\\Programs\\Python\\python-3')
Any friends can teach me how to build a virtualenv with python 3.7.7?

I think you haven't installed virtual environment in your local python
pip install virtualenv
and follow your steps. Stil, find the error. Try doing below methods, I think you will get your answer-
in command prompt
pip install virtualenv
go to the location, where you want to create your environment
cd location
virtualenv project_env_name
Now, you will find a python env in the desired location, Then go to scripts
cd project_env_name/scripts
activate
You will enter the environment you created. while leaving the environment, do
deactivate
To leave the environment.
This method works if you want to create the same python version environment as python version in your machine.
If you want to create an environment of the different version, you need to install the python of that version.

Related

How to create a Python virtual environment independent of OS and Python version

I am trying to create a virtual environment to run a script which requires Python 3.6. I started off with Pipenv but I am unable to create the same environment on other platforms via the Pipfile.lock or requirements.txt unless the other platform(s) has Python 3.6 installed. I have read this post but I am unsure which direction I should take to create a virtual environment which can be shared and run its own version of Python independent of operating system and version of Python installed on the other platform.
Virtual environments are not portable, they depend on the Python installation you have.
You can't share/distribute virtual environment with others, because you can't control which version of Python others are using.
If you want to distribute your code along with all dependencies including the specific version of Python interpreter, you can use PyInstaller. It is far from perfect and little bit hacky. Also it generates a package which is specific to operating system.
https://pyinstaller.readthedocs.io/en/stable/operating-mode.html
There is also a detailed step-by-step guide on how to use PyInstaller.
https://realpython.com/pyinstaller-python/
This is step-by-step how I use Python virtual environment and share it with co-workers.
To check python and virtualenv presence, run following commands:
which python3
python3 -m pip list | grep env
which virtualenv
Install a python virtual environment builder:
python3 -m pip install virtualenv
Create a virtual environment named venv inside the project's directory: virtualenv venv
To activate this environment use this command inside project's directory: source venv/bin/activate
Install python modules dependencies listed in a requirements.txt:
python3 -m pip install -r requirements.txt
You should activate virtual environment when you working with python in this directory for package installation and for running commands in the project directory. When you need to deactivate the virtual environment do it using deactivate command.
To deactivate environment simply run: deactivate

install packages in Python2 with Python3

I am trying to install numpy, nltk, etc packages for Python 2 to run a code. But I have Python3 as well and the path variable is set to it. When I try to use any pip install command it shows the package is available in Python3's directory.
Also, I am using VSCode, so I did not add the path variable.
I suggest you use virtual environments. Because if you read about virtual environments, you will find that they are created for such cases.
To create virtual environments, you must do the following:
Make a note of the full file path to the custom version of Python you just installed.
virtualenv -p /home/username/opt/python-2.7.15/bin/python venv
In order to use this environment’s packages/resources in isolation, you need to “activate” it. To do this, just run the following:
source venv/bin/activate (Linux)
./venv/Scripts/activate.bat (Windows)
Notice how your prompt is now prefixed with the name of your environment (venv, in our case). This is the indicator that venv is currently active, which means the python executable will only use this environment’s packages and settings.
Now run the following:
(venv) $ which python
/Users/ashkan/python-virtual-environments/venv/bin/python (in my case)
now you have access to python2.7.
The best practice for this particular problem would be virtual environments.And for that matter Pipenv would be a good option.
Install Pipenv.
$ brew install pipenv (MacOs)
$ sudo apt install pipenv (Debian)
$ sudo dnf install pipenv (Fedora)
pip install pipenv (Windows)
Creating virtual env with Pipenv.
pipenv install --python 2.7 numpy
This command will install create a virtual environment and install python 2.7(which will be used as the main interpreter once you activate the environment) along with numpy in that environment. This will avoid the packages version conflicts too.
To activate the environment
pipenv shell
If you are working in the Vs Code workspace then you should set the interpreter path(python path) to the path of the virtual environment.
when we install anything using pip. it will install dependencies for default python version. so you can change the default python version using this link https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux
Hope this will solve your problem
After crating a virtual environment with python 2.7 you can install your required packages

How to install Python packages in a specific environment?

I installed Anaconda3 so I can create environments and install different packages in each environment. But I fail to understand the difference between the Python in
/usr/bin/python
and
/opt/anaconda3/bin/python
I can seem to access Python 3.6.5 Anaconda from both, why is that? And, what is the difference between both?
Furthermore, I would like to install packages to a single Python environment only.
When you are running python in the terminal, it is looking up your default path to your the python command. In this case, anaconda probably put a line in your shell profile specify the path to the anaconda version, which is why you are seeing it in the interpreter when you run python from either directory.
Secondly, you can set up a conda environment to download app specific dependencies without interfering with your default set up by
conda create --name myenv
source activate myenv
conda install packagename
This will install it in the myenv environment only. To deactivate the environment just run
source deactivate
Here is the documentation on that https://conda.io/docs/user-guide/tasks/manage-environments.html
Judging by your path, you are using Linux which comes with python installed. So /usr/bin/python is default and you have installed the other one later.
For the environments use https://conda.io/docs/user-guide/tasks/manage-environments.html to activate the desired environment, then you can pip install or conda install the packages and it will be places safely only in that environment. Note that spyder icon runs the root environment by default and you have to run it from terminal after activating one of the environments.
Edit:
I'm not sure why you want to use cd to change the python version. I suggest use aliases. I guess you are just changing the path but running the same version of the python anyway. Take a look at this question:
Two versions of python on linux. how to make 2.7 the default
I wanted to create a new virtual environment to install new packages. Following worked for me:
Commands are executed in Jupyter Notebook (OS: Ubuntu 16.04 LTS)
Upgrade pip:
!pip install --upgrade pip
Install virtual environment:
!pip install virtualenv
Select version of Python you want to use in new environment:
I wanted to create an environment with Python version 3. Naming it as Python3_xyz:
!virtualenv -p python3 Python3_xyz
After execution, this will create a folder with the same name in the current working directory (i.e. the location where Jupyter notebook is present)
Create a new option with the name of the created environment
And finally, run the following command:
!python -m ipykernel install --user --name=Python3_xyz
This will create a new option with the name Python3_xyz in the menu from where we create a new notebook.
NOTE: One can run above commands from the terminal as well just don't use '!' before the commands.
This question is bit dated, but since I faced a similar issue, what worked for me might help someone!
I did pip install requests from within my conda environment, but failed to import requests even after trying out everything.
What worked for me: run python -m pip install requests or python3 -m pip install requests within you environment. This installed requests successfully for me.

How to use a virtual environment

Using Python I require both python 2.7 and python 3.5 for different packages. I am trying to install the following package NepidemiX. I get an error when I do this as I have a newer version of python installed.
To combat this I am trying to create a virtual environment. To do this I am using the virtualenv package.
I have created and activated this and am now faced with
(my_project)Your-Computer:your_project UserName$)
In my terminal.
How do I now proceed to install my package from here? Do I need to install python 2.7 in this environment first, or do I simply copy the desired package into the environment ... ?
Please could you instruct me how to correctly set this up?
Many thanks!
Virtual environment is only for libraries. It uses python versions installed on your computer. You can specify the version of python by using the -p attribute while creating the environment, for ex. virtualenv -p python3 env creates a python 3 enviroment (provided you have it installed in your computer and on the PATH). Check this answer.
After you activate the environment (source /env/bin/activate), just pip install libraries, and the environment takes care of installing the correct version.

Anaconda As Python in Debian Linux Terminal

I am working on a project in python on a Debian 8 VM. To work on the project further I need to install matplotlib 1.5.1. When I attempt to upgrade the current version (obtained through apt-get) or install I am told that I need freetype and png. When I go to install freetype using this link:
http://www.linuxfromscratch.org/blfs/view/svn/general/freetype2.html
After installing and entering the proper commands, I go to try to install matplotlib again and receive the same error.
I tried to install Anaconda3 because it comes with freetype and basically every package that I need for my project. But after running the .sh file I was unable to change my python to use anaconda as the interpreter. How can I do this?
Thanks!
[UPDATE]
I am having to go into my anaconda3 file, then run source bin/activate ~/anaconda3/ Is there anyway to create an alias that would do all this?
You have to first create a conda python environment:
/path/to/conda/bin/conda create --name myenv python=3
(see http://conda.pydata.org/docs/using/envs.html)
When the environment has been created you simply activate it as follows:
/path/to/conda/bin/source activate myenv
Thereafter the system will run python from the conda environment you specified and not from the standard location.

Categories