I want to write a first_setup.py script, where users can run it for the first time and the script will do the entire setup automatically, namely it will create a virtual environment, activate it, and install the requirements with pip. Users can then start to use the program without worrying about any dependency issue.
Therefore, I used venv together with subprocess library. My Python version is 3.7.5. With the following command, it creates the virtual environment in the working directory:
env_name = ".venv"
subprocess.run(["python", "-m", "venv", env_name])
However, activation doesn't work. I tried to activate it in various ways, for example:
subprocess.run([f"{venv_name}\\Scripts\\Activate.ps1"], shell=True)
This just opens Activate.ps1 in Windows Text Editor like a .txt file (?). I also thought to add .../Scripts/python.exe to the PATH variable, but it didn't work actually.
Furthermore, when the venv created by the script, VS Code pops up a message saying a venv detected, do you want to use it? If I click on Yes, then it changes my interpreter to venv, which is exactly what I want to do, but automatically with the first_setup.py script.
How can I proceed?
Does this first_setup script need to be Python?
The problem is that the activate script sets environment variables for the shell, which is why it’s usually run with ‘source’.
try using bash file:
python3 -m venv venv
source venv/bin/activate
pip install your_library
I am on a Mac. The terminal window doesn't seem to be able to access my bin or bash folder anymore, I'm not sure.
In the terminal window I am typing conda --version to determine which version of Anaconda I have installed but I get the message -bash: conda: command not found. - Also when I am in Atom, and am using the Python text editor and I'm trying to create a virtual environment, I type sudo conda create --name myDjangoEnv django and I get the same message, sudo: conda: command not found.
I used sudo because otherwise it would not let me set up a virtual environment without entering a password.
I think I edited the bash profile when I set up the virtual environment.
The thing is, I was able to do all of this yesterday, when I typed conda --version in my terminal I got the version of anaconda I installed, and I was able to update the version of anaconda. And in the python editor in Atom, when I typed sudo conda create --name myDjangoEnv django it set up a virtual environment. I was also able to update to the latest version of django.
Additionally, I had a new problem. In Atom I couldn't install a package. if I hit install a package, nothing would happen. Yesterday I was able to install packages. However I restarted Atom and now I am able to install packages, but I'm still having the problem listed above.
Does anyone know what is going on? I'm pretty new to all of this stuff. Should I reinstall Atom, Anaconda, django, and Python? Perhaps, I didn't install it to the right location? Is it easier to run all these programs on windows 10?
Atom is installed in /Users/myusername/Downloads
django: there's no .app for django so its hard to tell where its installed. Some of the folders for django are installed here:
/Users/myusername/newproject/newenv/lib/python3.7/site-packages
or another django folder's location is here:
/Users/myusername/opt/anaconda3/lib/python3.8/site-packages/bokeh/server
python: there are so many python folders, 1 python 3.8 folder's location is:
/Users/myusername/opt/anaconda3/pkgs/lxml-4.5.2-py38h63b7cb6_0/lib
Anaconda: /Users/myusername/opt/anaconda3
echo "$PATH" produces
/Volumes/myExternalDrive/opt/anaconda3/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
conda is installed in: /Users/myUserName/opt/anaconda3/pkgs/conda-4.8.3-py38_0/info/test/tests/test-recipes/activate_deactivate_package/src/etc –
Your PATH appears to have an incorrect component. Where you see
/Volumes/myExternalDrive/opt/anaconda3/bin
you should apparently have
/Users/myusername/opt/anaconda3/bin
At a terminal prompt, you can temporarily fix this with
PATH=/Users/myusername/opt/anaconda3/bin:$PATH
and at that point, you should be able to run conda --version and python3 in that terminal instance, until you terminate it.
The proper permanent fix is to find the place where this was incorrectly configured, and correct it there; probably your .bashrc needs a more thorough cleanup, but that's already far outside the scope of your original question.
(This assumes your home directory is /Users/myusername; the command echo "$HOME" should confirm this. That's where you'd look for .bashrc too.)
I want to use python 2.7 instead of python 3, which is used by default (note that I have them both installed on my computer already).
So I tried what I saw should be the right thing to do, that is:
python -m virtualenv my_env -p "\Program Files (x86)\Python27\pythonw.exe"
and I receive the following message:
Running virtualenv with interpreter \Program Files (x86)\Python27\pythonw.exe
That doesn't look so much like an error to me, but yet, no environment is created. Can someone help me with that?
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.
So far I've been able to work with Python 3 and Xcode 5.1.1 by editing the scheme, pointing the Run executable to python3 and then adding some arguments as in the following picture:
But now I would like to work with isolated Python environments through virtualenv. I can do this through the terminal, as I just have to type workon pydev where pydev is my Python environment. But I have no idea of how to modify my Xcode configuration so that I can run within a Python environment. Is there an environmental variable or something I can set to make this happen?
UPDATE
So I have a partial answer but I still can't make it work on Xcode. I created a simple bash script:
#!/usr/bin/env bash
source ~/.bash_profile
workon pydev
python3 src/__main__.py
The source is needed because otherwise the workon command doesn't work (another file provided by the virtualenv package is sourced inside .bash_profile. I can run the script in the Terminal, but the problem now is that Xcode doesn't like it:
Xcode cannot run using the selected destination.
Choose a destination with a supported architecture in order to run on this system.
I solved it, so you have to add the following pre-action to Xcode.
The source is needed because otherwise the workon command doesn't work (another file provided by the virtualenv package is sourced inside .bash_profile.